如何检查日期是否在给定范围内? [英] How to check if a date is in a given range?

查看:110
本文介绍了如何检查日期是否在给定范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有一个 $ start_date $ end_date ,那么您如何检查用户是否给出了一个日期落在该范围内?

If you have a $start_date and $end_date, how can you check if a date given by the user falls within that range?

例如

$start_date = '2009-06-17';

$end_date = '2009-09-05';

$date_from_user = '2009-08-28'; 

在这些日期是字符串的时候,是否有助于将它们转换为时间戳整数?

At the moment the dates are strings, would it help to convert them to timestamp integers?

推荐答案

将它们转换为时间戳是正确的方法,使用 strtotime ,例如

Converting them to timestamps is the way to go alright, using strtotime, e.g.

$start_date = '2009-06-17';

$end_date = '2009-09-05';

$date_from_user = '2009-08-28';

check_in_range($start_date, $end_date, $date_from_user);


function check_in_range($start_date, $end_date, $date_from_user)
{
  // Convert to timestamp
  $start_ts = strtotime($start_date);
  $end_ts = strtotime($end_date);
  $user_ts = strtotime($date_from_user);

  // Check that user date is between start & end
  return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));
}

这篇关于如何检查日期是否在给定范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆