R2D*_*2D2 41 php time date strtotime
我想检查,如果日期是今天,明天,昨天或其他.但我的代码不起作用.
码:
$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
}
Run Code Online (Sandbox Code Playgroud)
代码总是在其他情况下.
Nic*_*lai 43
第一.您在使用函数时遇到错误,strtotime请参阅PHP文档
int strtotime ( string $time [, int $now = time() ] )
Run Code Online (Sandbox Code Playgroud)
您需要修改代码以将整数时间戳传递到此函数中.
第二.您使用格式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";
Run Code Online (Sandbox Code Playgroud)
您需要检查是否strtotime($timestamp)返回正确的日期字符串.如果不是,则需要指定$timestamp变量中使用的格式.您可以使用函数之一date_parse_from_format或DateTime :: createFromFormat来完成此操作
这是一个工作示例:
$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";
}
Run Code Online (Sandbox Code Playgroud)
Nit*_*ith 23
<?php
$current = strtotime(date("Y-m-d"));
$date = strtotime("2014-09-05");
$datediff = $date - $current;
$difference = floor($datediff/(60*60*24));
if($difference==0)
{
echo 'today';
}
else if($difference > 1)
{
echo 'Future Date';
}
else if($difference > 0)
{
echo 'tomorrow';
}
else if($difference < -1)
{
echo 'Long Back';
}
else
{
echo 'yesterday';
}
?>
Run Code Online (Sandbox Code Playgroud)
小智 10
我想这会对你有所帮助:
<?php
$date = new DateTime();
$match_date = new DateTime($timestamp);
$interval = $date->diff($match_date);
if($interval->days == 0) {
//Today
} elseif($interval->days == 1) {
if($interval->invert == 0) {
//Yesterday
} else {
//Tomorrow
}
} else {
//Sometime
}
Run Code Online (Sandbox Code Playgroud)
Sha*_*roq 10
今天/昨天/明天的简单单行:
$someDate = '2021-11-07'; // date in any format
$isToday = date('Ymd') == date('Ymd', strtotime($someDate));
$isYesterday = date('Ymd') == date('Ymd', strtotime($someDate) + 86400);
$isTomorrow = date('Ymd') == date('Ymd', strtotime($someDate) - 86400);
Run Code Online (Sandbox Code Playgroud)
在Php(shame ^^)中没有内置函数可以做到这一点.您想要将日期字符串与今天进行比较,您可以使用简单的方法substr来实现它:
if (substr($timestamp, 0, 10) === date('Y.m.d')) { today }
elseif (substr($timestamp, 0, 10) === date('Y.m.d', strtotime('-1 day')) { yesterday }
Run Code Online (Sandbox Code Playgroud)
没有日期转换,简单.
小智 6
function getRangeDateString($timestamp) {
if ($timestamp) {
$currentTime=strtotime('today');
// Reset time to 00:00:00
$timestamp=strtotime(date('Y-m-d 00:00:00',$timestamp));
$days=round(($timestamp-$currentTime)/86400);
switch($days) {
case '0';
return 'Today';
break;
case '-1';
return 'Yesterday';
break;
case '-2';
return 'Day before yesterday';
break;
case '1';
return 'Tomorrow';
break;
case '2';
return 'Day after tomorrow';
break;
default:
if ($days > 0) {
return 'In '.$days.' days';
} else {
return ($days*-1).' days ago';
}
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)