How to convert cron expression from local timezone to UTC in PHP-ThrowExceptions

Exception or error:

I have a PHP application, where there is a HTML form that takes CRON Expression and Local Timezone as input from users. I want to convert that CRON expression in local timezone to UTC timezone before saving it in DB, and probably do vice versa, when user wants to update their CRON expression/timezone.

Something like how we can convert date using gmdate function.

For example, 0 6 * * * in Asia/Kolkata (+5.5) timezone should convert to 30 0 * * * in UTC.

What I have tried?

I have tried separately capturing the first 2 values in the CRON expression, which basically comprise the hour and minutes and then converted them like this.

$schedule_cron_hour = $_POST['cron-time-hour'];
$schedule_cron_min = $_POST['cron-time-min'];

file_put_contents('2.txt', $schedule_cron_hour.":".$schedule_cron_min."  ".$schedule_cron."  ".$schedule_timezone);


date_default_timezone_set($schedule_local_timezone);


$schedule_cron_time = strtotime($schedule_cron_hour.":".$schedule_cron_min);

file_put_contents('1.txt', gmdate("H:i",$schedule_cron_time));

Is there any function, or a efficient solution to achieve this?

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *