Re: Conversion between Unix and Localized Datetime

From: Date: Tue, 04 Oct 2016 16:03:17 +0000
Subject: Re: Conversion between Unix and Localized Datetime
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-325767@lists.php.net to get a copy of this message
Hi, Narcis: You are welcome. The DateTime object is quite handy at the tasks of date and time operations. -- Thanks -------------------- Jack * Narcis Garcia <informatica@actiu.net> [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: [application/pgp-signature] signature.asc
« previous php.general (#325767) next »