PHP check if date between two dates

匿名 (未验证) 提交于 2019-12-03 01:08:02

问题:

I got this code from Stackoverflow and changed it slightly to work with today's date.

I want to check if today fits between two dates. But this is not working. What am I missing?

$paymentDate = date('d/m/Y'); echo $paymentDate; // echos today!  $contractDateBegin = date('d/m/Y', '01/01/2001'); $contractDateEnd = date('d/m/Y', '01/01/2015');  if ($paymentDate > $contractDateBegin && $paymentDate 

回答1:

This is the right answer for your code.. Just use strtotime() php function

    $paymentDate = date('Y-m-d');     $paymentDate=date('Y-m-d', strtotime($paymentDate));;     //echo $paymentDate; // echos today!      $contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));     $contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));      if (($paymentDate > $contractDateBegin) && ($paymentDate 


回答2:

You cannot compare date-strings. It is good habit to use PHP's DateTime object instead:

$paymentDate = new DateTime(); // Today echo $paymentDate->format('d/m/Y'); // echos today!  $contractDateBegin = new DateTime('2001-01-01'); $contractDateEnd  = new DateTime('2015-01-01');  if (   $paymentDate->getTimestamp() > $contractDateBegin->getTimestamp() &&    $paymentDate->getTimestamp() getTimestamp()){   echo "is between"; }else{    echo "NO GO!";   }


回答3:

If hours matter:

$paymentDate = strtotime(date("Y-m-d H:i:s")); $contractDateBegin = strtotime("2014-01-22 12:42:00"); $contractDateEnd = strtotime("2014-01-22 12:50:00");  if($paymentDate > $contractDateBegin && $paymentDate 


回答4:

You are comparing the dates as strings, which won't work because the comparison is lexicographical. It's the same issue as when sorting a text file, where a line 20 would appear after a line 100 because the contents are not treated as numbers but as sequences of ASCII codes. In addition, the dates created are all wrong because you are using a string format string where a timestamp is expected (second argument).

Instead of this you should be comparing timestamps of DateTime objects, for instance:

 $paymentDate = date_create();  $contractDateBegin = date_create_from_format('d/m/Y', '01/01/2001');  $contractDateEnd = date_create_from_format('d/m/Y', '01/01/2015');

Your existing conditions will then work correctly.



回答5:

Use directly

$paymentDate = strtotime(date("d-m-Y")); $contractDateBegin = strtotime("01-01-2001"); $contractDateEnd = strtotime("01-01-2015");

Then comparison will be ok cause your 01-01-2015 is valid for PHP's 32bit date-range, stated in strtotime's manual.



回答6:

Based on luttken's answer. Thought I'd add my twist :)

function dateIsInBetween(\DateTime $from, \DateTime $to, \DateTime $subject) {     return $subject->getTimestamp() > $from->getTimestamp() && $subject->getTimestamp() getTimestamp() ? true : false; }  $paymentDate       = new \DateTime('now'); $contractDateBegin = new \DateTime('01/01/2001'); $contractDateEnd   = new \DateTime('01/01/2016');  echo dateIsInBetween($contractDateBegin, $contractDateEnd, $paymentDate) ? "is between" : "NO GO!";


回答7:

Another solution would have been to consider date written as Ymd.

Written in this "format" this is much easy to compare dates.

$paymentDate       = date('Ymd'); // on 4th may 2016, would have been 20160504 $contractBegin     = 20010101; $contractEnd       = 20160101; echo ($paymentDate >= $contractBegin && $paymentDate 

It will always work for every day of the year and do not depends on any function or conversion (PHP will consider the int value of $paymentDate to compare with the int value of contractBegin and contractEnd).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!