php 日期格式的日期比较 ('j F Y')
php Date Comaprisons for date format ('j F Y')
我有两个约会对象:
$today = date('j F Y');
$display_until = date('j F Y');
($display_until 技术上来自 ACF 字段日期选择器):
我目前正在使用:
<?php if($today < $display_until) : ?>
*where the magic should happen*
<?php endif; ?>
但它并没有按预期划分事件......任何人都可以指出我正确的方向吗?
根据您评论的进一步解释,您可以使用 php 的 strtotime
函数将任何类似日期的字符串转换为时间戳,稍后您可以将此时间戳传递给 date
函数以您想要的格式返回日期。
这是代码:
date('j F Y', strtotime('2017-05-01'));
如果要比较,那么:
if(strtotime('Jul 10, 2017') < strtotime('+10 days')) {
// Your logic
}
您也可以使用较新的 DateTime
对象。
你有两个解决方案:
1
将日期转换为时间戳然后进行比较:
$today = date('j F Y');
$display_until = date('j F Y');
if (strtotim($today) < strtotime($display_until)){
//
}
2
使用DateTime
对象:
$today = new DateTime();
$diplay_until = new DateTime(); // or new DateTime()->modify('+1 month');
if ($today < $diplay_until){
// do something
}
我有两个约会对象:
$today = date('j F Y');
$display_until = date('j F Y');
($display_until 技术上来自 ACF 字段日期选择器):
我目前正在使用:
<?php if($today < $display_until) : ?>
*where the magic should happen*
<?php endif; ?>
但它并没有按预期划分事件......任何人都可以指出我正确的方向吗?
根据您评论的进一步解释,您可以使用 php 的 strtotime
函数将任何类似日期的字符串转换为时间戳,稍后您可以将此时间戳传递给 date
函数以您想要的格式返回日期。
这是代码:
date('j F Y', strtotime('2017-05-01'));
如果要比较,那么:
if(strtotime('Jul 10, 2017') < strtotime('+10 days')) {
// Your logic
}
您也可以使用较新的 DateTime
对象。
你有两个解决方案:
1
将日期转换为时间戳然后进行比较:
$today = date('j F Y');
$display_until = date('j F Y');
if (strtotim($today) < strtotime($display_until)){
//
}
2
使用DateTime
对象:
$today = new DateTime();
$diplay_until = new DateTime(); // or new DateTime()->modify('+1 month');
if ($today < $diplay_until){
// do something
}