Re: Conversion between Unix and Localized Datetime

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi, Narcis:

You are welcome. The DateTime object is quite handy at the tasks of
date and time operations.

--
Thanks
--------------------
Jack
* Narcis Garcia <informatica@xxxxxxxxx> [2016-10-04 16:26:08 +0200]:

Thank you;
I've written now this function:

function DatetimeFromUnix($UnixTimeStamp, $TimezoneLabel)
{
	$Object = new DateTime('@'.$UnixTimeStamp);
	$Object->setTimeZone(new DateTimeZone($TimezoneLabel));
	return $Object->format('Y-m-d H:i:s T');
}

When I use DatetimeFromUnix(1475420421, 'Europe/Berlin')
It returns:
'2016-10-02 17:00:21 CEST'



El 04/10/16 a les 14:23, Jack ha escrit:
Hi, Narcis:

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 = 1475420421;
   echo my_datestring($timestamp, "Europe/Paris");

Hope, this code will help you.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux