检查时间戳是否是今天

发布于 2024-11-03 13:58:47 字数 131 浏览 1 评论 0原文

我有以下格式的时间戳(由于 PHP 的优点,可以轻松更改!)。

2011-02-12 14:44:00

检查此时间戳是否是今天使用的最快/最简单的方法是什么?

I've got a timestamp in the following format (Which can easily be changed thanks to the beauties of PHP!).

2011-02-12 14:44:00

What is the quickest/simplest way to check if this timestamp was taken today?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一杆小烟枪 2024-11-10 13:58:47

我认为:

date('Ymd') == date('Ymd', strtotime($timestamp))

I think:

date('Ymd') == date('Ymd', strtotime($timestamp))
玩物 2024-11-10 13:58:47
if (date('Y-m-d') == date('Y-m-d', strtotime('2011-02-12 14:44:00'))) {
    // is today
}
if (date('Y-m-d') == date('Y-m-d', strtotime('2011-02-12 14:44:00'))) {
    // is today
}
韶华倾负 2024-11-10 13:58:47
$offset = date('Z'); //timezone offset in seconds

if (floor(($UNIX_TIMESTAMP + $offset) / 86400) == floor((mktime(0,0,0) + $offset) / 86400)){
    echo "today";
}
$offset = date('Z'); //timezone offset in seconds

if (floor(($UNIX_TIMESTAMP + $offset) / 86400) == floor((mktime(0,0,0) + $offset) / 86400)){
    echo "today";
}
软的没边 2024-11-10 13:58:47

这就是我用于此类任务的方法:

/** date comparator restricted by $format.
 @param {int/string/Datetime} $timeA
 @param {int/string/Datetime} $timeB
 @param {string} $format
 @returns : 0 if same. 1 if $timeA before $timeB. -1 if after   */
function compareDates($timeA,$timeB,$format){
    $dateA=$timeA instanceof Datetime?$timeA:(is_numeric($timeA)?(new \Datetime())->setTimestamp($timeA):(new \Datetime("".$timeA)));
    $dateB=$timeB instanceof Datetime?$timeB:(is_numeric($timeB)?(new \Datetime())->setTimestamp($timeB):(new \Datetime("".$timeB)));
    return $dateA->format($format)==$dateB->format($format)?0:($dateA->getTimestamp()<$dateB->getTimestamp()?1:-1);
}

比较日期:$format='Ymd'。
比较月份:$format='Ym'。
等等......

在你的情况下:

if(compareDates("now",'2011-02-12 14:44:00','Y-m-d')===0){
    // do stuff
}

This is what i use for this kind of task :

/** date comparator restricted by $format.
 @param {int/string/Datetime} $timeA
 @param {int/string/Datetime} $timeB
 @param {string} $format
 @returns : 0 if same. 1 if $timeA before $timeB. -1 if after   */
function compareDates($timeA,$timeB,$format){
    $dateA=$timeA instanceof Datetime?$timeA:(is_numeric($timeA)?(new \Datetime())->setTimestamp($timeA):(new \Datetime("".$timeA)));
    $dateB=$timeB instanceof Datetime?$timeB:(is_numeric($timeB)?(new \Datetime())->setTimestamp($timeB):(new \Datetime("".$timeB)));
    return $dateA->format($format)==$dateB->format($format)?0:($dateA->getTimestamp()<$dateB->getTimestamp()?1:-1);
}

compare day : $format='Y-m-d'.
compare month : $format='Y-m'.
etc...

in your case :

if(compareDates("now",'2011-02-12 14:44:00','Y-m-d')===0){
    // do stuff
}
方觉久 2024-11-10 13:58:47

我更喜欢比较时间戳(而不是日期字符串),所以我今天用它来检查。

$dayString = "2011-02-12 14:44:00";
$dayStringSub = substr($dayString, 0, 10);

$isToday = ( strtotime('now') >= strtotime($dayStringSub . " 00:00") 
          && strtotime('now') <  strtotime($dayStringSub . " 23:59") );

小提琴:http://ideone.com/55JBku

I prefer to compare timestamps (rather then date strings), so I use this to check today.

$dayString = "2011-02-12 14:44:00";
$dayStringSub = substr($dayString, 0, 10);

$isToday = ( strtotime('now') >= strtotime($dayStringSub . " 00:00") 
          && strtotime('now') <  strtotime($dayStringSub . " 23:59") );

Fiddle: http://ideone.com/55JBku

四叶草在未来唯美盛开 2024-11-10 13:58:47
(date('Ymd') == gmdate('Ymd', $db['time']) ? 'today' : '')
(date('Ymd') == gmdate('Ymd', $db['time']) ? 'today' : '')
遇见了你 2024-11-10 13:58:47

你是这个意思吗?

if( strtotime( date( 'Y-m-d' , strtotime( '2011-02-12 14:44:00' ) ) ) == strtotime( date( 'Y-m-d' ) ) )
{
//IS TODAY;
}

are you mean this ?

if( strtotime( date( 'Y-m-d' , strtotime( '2011-02-12 14:44:00' ) ) ) == strtotime( date( 'Y-m-d' ) ) )
{
//IS TODAY;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文