admin管理员组文章数量:1130349
I am trying to schedule WP Cron that should run every midnight at Local Timestamp, but it is taking UTC only. This the code I am trying with,
if ( ! wp_next_scheduled( 'midnight_cron' ) ) {
$now = current_time('timestamp', 1 );
$time = strtotime('tomorrow', $now );
wp_schedule_event($time, 'daily', 'midnight_cron');
}
I know by default WP cron uses UTC/GMT time, not local time, but what could be the possible way to achieve this?
Update:
if ( ! wp_next_scheduled( 'midnight_cron' ) ) {
$gmt = get_option( 'gmt_offset' );
$time = strtotime('midnight') + ((24 - $gmt) * HOUR_IN_SECONDS);
if($time < time()) $time + (24 * HOUR_IN_SECONDS);
wp_schedule_event($time, 'daily', 'midnight_cron');
}
I am trying to schedule WP Cron that should run every midnight at Local Timestamp, but it is taking UTC only. This the code I am trying with,
if ( ! wp_next_scheduled( 'midnight_cron' ) ) {
$now = current_time('timestamp', 1 );
$time = strtotime('tomorrow', $now );
wp_schedule_event($time, 'daily', 'midnight_cron');
}
I know by default WP cron uses UTC/GMT time, not local time, but what could be the possible way to achieve this?
Update:
if ( ! wp_next_scheduled( 'midnight_cron' ) ) {
$gmt = get_option( 'gmt_offset' );
$time = strtotime('midnight') + ((24 - $gmt) * HOUR_IN_SECONDS);
if($time < time()) $time + (24 * HOUR_IN_SECONDS);
wp_schedule_event($time, 'daily', 'midnight_cron');
}
Share
Improve this question
edited Oct 20, 2018 at 21:05
Deepak Singh
asked Oct 20, 2018 at 20:20
Deepak SinghDeepak Singh
1488 bronze badges
2 Answers
Reset to default 3Part 1. The Time != The Timestamp
This is your problem:
every midnight at Local Timestamp
There is no such thing as a local timestamp. Timestamps are not timezoned, it's not that WP uses UTC, but rather that UTC timestamps just happen to be +0 hours.
If you want it to happen at midnight local time, you need to convert that time into a timestamp, and UTC is the easiest way to do that. For example midnight BST is 11pm UTC, so I would need to schedule a cron job for 11pm for it to run at midnight. If I set the cron job to run at 00:00, then that would be 1am local time, which is not what I wanted.
So take your desired localized timezoned time, and convert it to UTC in code. You can always undo the math at a later date if you need to display it.
e.g. here we take a UTC date and change it to Moscow time:
$date = new DateTime('2012-07-16 01:00:00 +00');
$date->setTimezone(new DateTimeZone('Europe/Moscow')); // +04
echo $date->format('Y-m-d H:i:s'); // 2012-07-15 05:00:00
And here we take a Bangkok time and convert it to UTC:
$given = new DateTime("2014-12-12 14:18:00 +07");
echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 14:18:00 Asia/Bangkok
$given->setTimezone(new DateTimeZone("UTC"));
echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 07:18:00 UTC
If you still want to specify things in local time just write a function to do the conversion, e.g. $utc_time = convert_to_utc( "time in local timezone")
Part 2. Why UTC?
Have you ever noticed why some timezones are referenced as +8 hours or -2? That would be UTC+8 or UTC-2. UTC is also known as coordinated universal time.
It's the time system used as the base standard that all the usual time systems we use day to day are derived from. So your local time is defined as some offset of UTC.
So Can I make WP use the local timezone for timestamps?
If WP stored everything using local time, then changing your local time would involve modifying every timestamp in the database. It would cause issues with communications with other APIs. It would also cause issues with plugins and themes that try to convert from UTC to your local timezone, doubling the offset, and causing compounding errors. It could also cause issues with the REST API, 2 factor auth, etc
It's a very bad idea. It also breaks the fundamental system of a timestamp on your site.
Think of it this way. You see times and dates in WordPress with a timezone modifier attached. When WP stores the data, it strips away the modifier, and stores it as a standardised value that all WP sites and computers understand. It then re-applies the timezone modifier whenever it displays the date.
The wp_schedule_event function has the schedule_event filter inside it. If you take a look at the wp-includes/cron.php lines 102-106 you can see how it is used.
Perhaps you could hook a custom function to that filter. In your custom filter function you could then do some local time check and short circuit the main function (i.e. return false), if it's the wrong time.
I haven't tested or done this myself before, but this idea just came to my mind.
I am trying to schedule WP Cron that should run every midnight at Local Timestamp, but it is taking UTC only. This the code I am trying with,
if ( ! wp_next_scheduled( 'midnight_cron' ) ) {
$now = current_time('timestamp', 1 );
$time = strtotime('tomorrow', $now );
wp_schedule_event($time, 'daily', 'midnight_cron');
}
I know by default WP cron uses UTC/GMT time, not local time, but what could be the possible way to achieve this?
Update:
if ( ! wp_next_scheduled( 'midnight_cron' ) ) {
$gmt = get_option( 'gmt_offset' );
$time = strtotime('midnight') + ((24 - $gmt) * HOUR_IN_SECONDS);
if($time < time()) $time + (24 * HOUR_IN_SECONDS);
wp_schedule_event($time, 'daily', 'midnight_cron');
}
I am trying to schedule WP Cron that should run every midnight at Local Timestamp, but it is taking UTC only. This the code I am trying with,
if ( ! wp_next_scheduled( 'midnight_cron' ) ) {
$now = current_time('timestamp', 1 );
$time = strtotime('tomorrow', $now );
wp_schedule_event($time, 'daily', 'midnight_cron');
}
I know by default WP cron uses UTC/GMT time, not local time, but what could be the possible way to achieve this?
Update:
if ( ! wp_next_scheduled( 'midnight_cron' ) ) {
$gmt = get_option( 'gmt_offset' );
$time = strtotime('midnight') + ((24 - $gmt) * HOUR_IN_SECONDS);
if($time < time()) $time + (24 * HOUR_IN_SECONDS);
wp_schedule_event($time, 'daily', 'midnight_cron');
}
Share
Improve this question
edited Oct 20, 2018 at 21:05
Deepak Singh
asked Oct 20, 2018 at 20:20
Deepak SinghDeepak Singh
1488 bronze badges
2 Answers
Reset to default 3Part 1. The Time != The Timestamp
This is your problem:
every midnight at Local Timestamp
There is no such thing as a local timestamp. Timestamps are not timezoned, it's not that WP uses UTC, but rather that UTC timestamps just happen to be +0 hours.
If you want it to happen at midnight local time, you need to convert that time into a timestamp, and UTC is the easiest way to do that. For example midnight BST is 11pm UTC, so I would need to schedule a cron job for 11pm for it to run at midnight. If I set the cron job to run at 00:00, then that would be 1am local time, which is not what I wanted.
So take your desired localized timezoned time, and convert it to UTC in code. You can always undo the math at a later date if you need to display it.
e.g. here we take a UTC date and change it to Moscow time:
$date = new DateTime('2012-07-16 01:00:00 +00');
$date->setTimezone(new DateTimeZone('Europe/Moscow')); // +04
echo $date->format('Y-m-d H:i:s'); // 2012-07-15 05:00:00
And here we take a Bangkok time and convert it to UTC:
$given = new DateTime("2014-12-12 14:18:00 +07");
echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 14:18:00 Asia/Bangkok
$given->setTimezone(new DateTimeZone("UTC"));
echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 07:18:00 UTC
If you still want to specify things in local time just write a function to do the conversion, e.g. $utc_time = convert_to_utc( "time in local timezone")
Part 2. Why UTC?
Have you ever noticed why some timezones are referenced as +8 hours or -2? That would be UTC+8 or UTC-2. UTC is also known as coordinated universal time.
It's the time system used as the base standard that all the usual time systems we use day to day are derived from. So your local time is defined as some offset of UTC.
So Can I make WP use the local timezone for timestamps?
If WP stored everything using local time, then changing your local time would involve modifying every timestamp in the database. It would cause issues with communications with other APIs. It would also cause issues with plugins and themes that try to convert from UTC to your local timezone, doubling the offset, and causing compounding errors. It could also cause issues with the REST API, 2 factor auth, etc
It's a very bad idea. It also breaks the fundamental system of a timestamp on your site.
Think of it this way. You see times and dates in WordPress with a timezone modifier attached. When WP stores the data, it strips away the modifier, and stores it as a standardised value that all WP sites and computers understand. It then re-applies the timezone modifier whenever it displays the date.
The wp_schedule_event function has the schedule_event filter inside it. If you take a look at the wp-includes/cron.php lines 102-106 you can see how it is used.
Perhaps you could hook a custom function to that filter. In your custom filter function you could then do some local time check and short circuit the main function (i.e. return false), if it's the wrong time.
I haven't tested or done this myself before, but this idea just came to my mind.
本文标签: plugin developmentHow to Trigger WP CRON at Local Timestamp
版权声明:本文标题:plugin development - How to Trigger WP CRON at Local Timestamp? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749245935a2338876.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论