PHP:如何检查日期是今天,昨天还是明天 [英] PHP: How to check if a date is today, yesterday or tomorrow
问题描述
如果日期是今天,明天,昨天或其他,我想检查。
但是我的代码不起作用。
代码:
$ timestamp =2014.09.02T13:34;
/ pre>
$ date = date(d.m.Y H:i);
$ match_date = date('d.m.Y H:i',strtotime($ timestamp));
if($ date == $ match_date){
//今天
} elseif(strtotime( - 1天,$ date )== $ match_date){
//昨天
} elseif(strtotime(+ 1天,$ date)== $ match_date){
//明天
} else {
//有时
}
代码总是处于其他情况。
解决方案strtotime.phprel =noreferrer> PHP文档
int strtotime(string $ time [,int $现在= time()])
您需要修改代码以将整数时间戳传递到此函数中。 / p>
第二。您使用包含时间部分的格式 dmY H:i 。如果您只想比较日期,则必须删除时间部分,例如`$ date = date(dmY);``
第三。我不知道它是否以同样的方式工作,但是我的PHP不明白
$ timestamp
的日期格式,并将 01.01.1970 02:00 返回到$ match_date
$ timestamp =2014.09.02T13:34;
date('d.m.Y H:i',strtotime($ timestamp))===01.01.1970 02:00;
您需要检查
strtotime($ timestamp)
返回正确的日期字符串。如果否,则需要指定在$ timestamp
变量中使用的格式。您可以使用以下功能之一执行此操作:date_parse_from_format
或 DateTime :: createFromFormat
这是一个工作示例:
$ timestamp = 2014.09.02T13:34;
$ today = new DateTime(); //此对象表示当前日期/时间
$ today-> setTime(0,0,0); //重置时间部分,以防止部分比较
$ match_date = DateTime :: createFromFormat(Y.m.d\\\THTH:i,$ timestamp);
$ match_date-> setTime(0,0,0); //重置时间部分,以防止部分比较
$ diff = $ today-> diff($ match_date);
$ diffDays =(integer)$ diff-> format(%R%a); //提取间隔的天数
switch($ diffDays){
case 0:
echo//今天;
break;
case -1:
echo// Yesterday;
break;
case +1:
echo// Tomorrow;
break;
默认值:
echo//有时;
}
I would like to check, if a date is today, tomorrow, yesterday or else. But my code doesn't work.
Code:
$timestamp = "2014.09.02T13:34"; $date = date("d.m.Y H:i"); $match_date = date('d.m.Y H:i', strtotime($timestamp)); if($date == $match_date) { //Today } elseif(strtotime("-1 day", $date) == $match_date) { //Yesterday } elseif(strtotime("+1 day", $date) == $match_date) { //Tomorrow } else { //Sometime }
The Code always goes in the else case.
解决方案First. You have mistake in using function
strtotime
see PHP documentationint strtotime ( string $time [, int $now = time() ] )
You need modify your code to pass integer timestamp into this function.
Second. You use format d.m.Y H:i that includes time part. If you wish to compare only dates, you must remove time part, e.g. `$date = date("d.m.Y");``
Third. I am not sure if it works in the same way for you, but my PHP doesn't understand date format from
$timestamp
and returns 01.01.1970 02:00 into$match_date
$timestamp = "2014.09.02T13:34"; date('d.m.Y H:i', strtotime($timestamp)) === "01.01.1970 02:00";
You need to check if
strtotime($timestamp)
returns correct date string. If no, you need to specify format which is used in$timestamp
variable. You can do this using one of functionsdate_parse_from_format
or DateTime::createFromFormatThis is a work example:
$timestamp = "2014.09.02T13:34"; $today = new DateTime(); // This object represents current date/time $today->setTime( 0, 0, 0 ); // reset time part, to prevent partial comparison $match_date = DateTime::createFromFormat( "Y.m.d\\TH:i", $timestamp ); $match_date->setTime( 0, 0, 0 ); // reset time part, to prevent partial comparison $diff = $today->diff( $match_date ); $diffDays = (integer)$diff->format( "%R%a" ); // Extract days count in interval switch( $diffDays ) { case 0: echo "//Today"; break; case -1: echo "//Yesterday"; break; case +1: echo "//Tomorrow"; break; default: echo "//Sometime"; }
这篇关于PHP:如何检查日期是今天,昨天还是明天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!