I try almost everything.. but wp_next_scheduled('hook_task')
always return time 1 hours back (my local timezone is UTC + 1, task scheduler is set for run each minute).
I try this conversions:
echo gmdate('Y-m-d H:i:s',wp_next_scheduled('hook_task'));
echo date('Y-m-d H:i:s',current_time(wp_next_scheduled('hook_task')));
echo date('Y-m-d H:i:s',current_time(wp_next_scheduled('hook_task'), 1));
but everything return 19:05
when next function return correct local time 20:05:
echo date('Y-m-d H:i:s',current_time("timestamp"));
date_default_timezone_get()
return UTC.
How to get correct local datetime 20:05 for next planned run?
Testing
I’ve tried based on your answers:
1) First test
echo 'ACTUAL: ' . date('Y-m-d H:i:s',current_time("timestamp")) . '<br>';
$date = new DateTime("now", new DateTimeZone('Europe/Bratislava'));
echo 'WITH TZ: ' . $date->format('Y-m-d H:i:s') . '<br>';
$time = wp_next_scheduled('hook_task');
$date = new DateTime("@$time", new DateTimeZone('Europe/Bratislava'));
echo 'RESULT: ' . $date->format('Y-m-d H:i:s');
I tried also this: $date = new DateTime("@$time", new DateTimeZone('Asia/Bangkok'));
but regardless I set as timezone it always return the same result.
And result:
ACTUAL: 2015-12-12 22:45:10 // correct
WITH TZ: 2015-12-12 22:45:10 // correct
RESULT: 2015-12-12 21:45:39 // incorrect
2) Correct solution
$time = wp_next_scheduled('hook_task');
echo 'RESULT2: ' . get_date_from_gmt( date('Y-m-d H:i:s', $time) );
And result:
RESULT2: 2015-12-12 22:45:39 // correct
WordPress uses GMT timestamp to reschedule the task every time it’s been executed, so in order to convert the timestamp in your local one you need to add the offset for your local time from GMT time.
Luckily there’s a function called
get_date_from_gmt
thatHere’s the code: