admin管理员组文章数量:1122832
I have made a custom theme and used CPT UI to create Event Post Type and ACF to create Event Date Field. I am using following code to display Remaining Event Days in frontend:
$event_date = strtotime(get_field('event_date', false, false));
$curr_date = time();
$rem_days = $event_date - $curr_date;
if ($rem_days <= 0) {
$event_msg = '<strong>Event Expired</strong>';
} else {
$event_msg = '<strong>' . date('d', $rem_days) . '</strong> Days Remaining';
}
The problem is events in this month are having right remaining days but events with next months are displaying less remaining days.
I don't know what I am missing out. Can you guys please help me?
You can see in screenshot Event with Oct 12 is displaying 23 days remaining, which is right. But event with Nov 3 is displaying just 14 days.
I have made a custom theme and used CPT UI to create Event Post Type and ACF to create Event Date Field. I am using following code to display Remaining Event Days in frontend:
$event_date = strtotime(get_field('event_date', false, false));
$curr_date = time();
$rem_days = $event_date - $curr_date;
if ($rem_days <= 0) {
$event_msg = '<strong>Event Expired</strong>';
} else {
$event_msg = '<strong>' . date('d', $rem_days) . '</strong> Days Remaining';
}
The problem is events in this month are having right remaining days but events with next months are displaying less remaining days.
I don't know what I am missing out. Can you guys please help me?
You can see in screenshot Event with Oct 12 is displaying 23 days remaining, which is right. But event with Nov 3 is displaying just 14 days.
Share Improve this question edited Sep 19, 2024 at 16:22 Anh Tuan Hoang 1886 bronze badges asked Sep 19, 2024 at 6:23 Milan BastolaMilan Bastola 2972 gold badges4 silver badges15 bronze badges1 Answer
Reset to default 4What you did wrong here is the part date('d', $rem_days)
. The function date()
should be used to convert timestamp to a formatted date, not to converting a time difference in timestamp to time difference in days.
You can fix this by replacing date('d', $rem_days)
with floor($rem_days/86400)
.
The complete code should be:
$event_date = strtotime( get_field( 'event_date', false, false ) );
$curr_date = time();
$rem_days = $event_date - $curr_date;
if ( $rem_days <= 0 ) {
$event_msg = '<strong>Event Expired</strong>';
} else {
$event_msg = '<strong>' . floor( $rem_days / 86400 ) . '</strong> Days Remaining';
}
Note: As per Pat J's comment, WordPress provides a bunch of handy {timespan}_IN_SECONDS
constants in wp-includes/default-constants.php
, including DAY_IN_SECONDS
.
So in the above code, floor( $rem_days / 86400 )
can be replaced with floor( $rem_days / DAY_IN_SECONDS )
本文标签: phpDate not working correctly
版权声明:本文标题:php - Date not working correctly 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290603a1928548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论