Re: Conversion between Unix and Localized Datetime
| From: | Ryan Pallas | Date: | Tue, 04 Oct 2016 12:59:18 +0000 |
| Subject: | Re: Conversion between Unix and Localized Datetime | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-325765@lists.php.net to get a copy of this message | ||
You could of course just use the date function[1] which is specifically
designed for this. Something like date('Y-m-d H:i:s', $timestamp) should do
the trick.
[1] http://www.php.net/date
On Tue, Oct 4, 2016 at 6:23 AM, Jack <jack@thinkingcloud.info> wrote:
> 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.
>
> --
> Thanks
> --------------------
> Jack
> * Narcis Garcia <informatica@actiu.net> [2016-10-04 12:40:36 +0200]:
>
>
> Hello,
>>
>> I have a unix timestamp such as 1475420421, 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(1475420421, 'Europe/Paris') ?
>>
>> Thanks.
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>