Re: Conversion between Unix and Localized Datetime
| From: | Narcis Garcia | Date: | Tue, 04 Oct 2016 14:26:08 +0000 |
| Subject: | Re: Conversion between Unix and Localized Datetime | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-325766@lists.php.net to get a copy of this message | ||
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.
>