top button
Flag Notify
Site Registration

Conversion between Unix and Localized Datetime using PHP

+1 vote
389 views

I have a unix timestamp such as **********, and want to convert it to a string like '2016-10-02 15:00:21' in a procedural way and without changing session/objects timezone, and independently of server/software/client timezones.

Is there some function that allows to format this, calling it as MyDateString(**********, 'Europe/Paris') ?

posted Oct 4, 2016 by Tarun Singhal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Why not use DateTime? The code you want is something like this:

 function my_datestring($timestamp, $timezone = "Asia/Shanghai", $format = "Y-m-d h:i:s") {
 $dt = new DateTime();
 $dt->setTimezone(new DateTimeZone($timezone));
 $dt->setTimestamp($timestamp);
 return $dt->format($format);
 }

 $timestamp = **********;
 echo my_datestring($timestamp, "Europe/Paris");

Hope, this will help you.

answer Oct 4, 2016 by Sonu Jindal
...