Home > front end >  PHP Convert date Timezone to Epoch
PHP Convert date Timezone to Epoch

Time:11-02

Any help with PHP to convert the below, taking into consideration the time zone at the end, to UNIX epoch for MYSQL insert.

29/05/2022 22:23:04 00:00

No examples of attempts :-/ Tried basic strtotime on it's own.

CodePudding user response:

Just parse the date and call getTimestamp.

$unixtime = DateTime::createFromFormat('d/m/Y H:i:s P', '29/05/2022 22:23:04  00:00')->getTimestamp();

echo $unixtime;

Output:

1653862984

CodePudding user response:

In the interest of helping someone out in a pinch .. You need to do something like this. I don't know your exact timezones, if they are 0:00 or 00:00 .. But this will get you started as a stand-alone app that you can form into your own script...

<?php
$zones =array(
'-10:00' => 'America/Atka',
'-09:00' => 'America/Yakutat',
'-08:00' => 'Mexico/BajaNorte',
'-07:00' => 'Mexico/BajaSur',
'-06:00' => 'Mexico/General',
'-05:00' => 'Canada/Eastern',
'-04:-30' => 'America/Caracas',
'-04:00' => 'Chile/Continental',
'-03:-30' => 'Canada/Newfoundland',
'-03:00' => 'Brazil/East',
'-02:00' => 'Brazil/DeNoronha',
'-01:00' => 'Atlantic/Cape_Verde',
' 00:00' => 'Europe/London',
' 01:00' => 'Europe/Zurich',
' 02:00' => 'Europe/Zaporozhye',
' 03:00' => 'Indian/Mayotte',
' 03:30' => 'Asia/Tehran',
' 04:00' => 'Indian/Reunion',
' 04:30' => 'Asia/Kabul',
' 05:00' => 'Indian/Maldives',
' 05:30' => 'Asia/Kolkata',
' 05:45' => 'Asia/Katmandu',
' 06:00' => 'Indian/Chagos',
' 06:30' => 'Indian/Cocos',
' 07:00' => 'Indian/Christmas',
' 08:00' => 'Australia/West',
' 08:45' => 'Australia/Eucla',
' 09:00' => 'Asia/Yakutsk',
' 09:30' => 'Australia/Yancowinna',
' 10:00' => 'Australia/Victoria',
' 10:30' => 'Australia/Lord_Howe',
' 11:00' => 'Asia/Magadan',
' 12:00' => 'Asia/Kamchatka'
);



$my_time = '29/05/2022 22:23:04  00:00';

$time_split = explode(' ', $my_time);
$my_date = $time_split[0];
$my_time = $time_split[1];
$my_zone = $time_split[2];


$datetime = DateTime::createFromFormat('d/m/Y G:H:s', "$my_date $my_time");
$timezone = new DateTimeZone($zones[$my_zone]);
$datetime->setTimezone($timezone);
echo "Your Date:Time: " . $datetime->format('F d, Y H:i') . "\n";
echo "Your EPOCH: " . strtotime($datetime->format('F d, Y H:i')) . "\n";
  •  Tags:  
  • php
  • Related