Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve a unix timestamp from DateTime object within last hour before DST switchover in PHP?

There is an example code

<?php
$dt = new \DateTime();
$dt->setTimezone(new \DateTimeZone('Europe/London'));

$timestamp = 1351383400;    
echo "$timestamp \n";

$dt->setTimestamp($timestamp);
echo $dt->getTimestamp();

which outputs 2 different timestamps 1351383400 (Sun, 28 Oct 2012 01:16:40 +0100, right before DST switch) and 1351387000 (Sun, 28 Oct 2012 01:16:40 +0000, 1 hour later, after DST switch)

The question is how can I make the Timestamp getter to return me exactly the same integer that has been passed to the Timestamp setter 1 row before ?

PHP 5.3.6

like image 652
Alex Blex Avatar asked Oct 26 '12 14:10

Alex Blex


People also ask

What is Unix timestamp in Linux?

A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC. When you run the program, the output will be: Here, we have imported datetime class from the datetime module. Then, we used datetime.fromtimestamp () classmethod which returns the local date and time (datetime object).

How to store date and time as timestamp in a database?

It's pretty common to store date and time as a timestamp in a database. A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC. Example 1: Python timestamp to datetime from datetime import datetime timestamp = 1545730073 dt_object = datetime.fromtimestamp(timestamp) print("dt_object =", dt_object) print ...

How to format a Unix timestamp to a readable date in PHP?

Welcome to a quick tutorial on how to format a Unix Timestamp to a readable date and time in PHP. Need to get a “human-friendly” date from a Unix Timestamp? We can use the date function to format a Unix Timestamp in PHP. To the ISO 8601 format – $formatted = date (DateTimeInterface::ATOM, $UNIX);

How do I change the timezone of a Unix timestamp in Python?

By default, the date () function will assume UTC+0 when parsing a Unix Timestamp. To set/change the timezone, we have to do it the “long-winded” way. Create a $tz = new DateTime () object. Then set it to the Unix Timestamp – $tz->setTimestamp ($UNIX). At this point, we can do a $formatted = $tz->format ("FORMAT") to create a date string.


1 Answers

Hope this helps:

    $dt = new DateTime();
    $dt->setTimezone(new \DateTimeZone('Europe/London'));
    $timestamp = 1181503727;
    echo $timestamp . '<br />';

    $dt->setTimestamp($timestamp);
    echo $dt->format('U'); 

output: 1181503727 1181503727

like image 148
Joe Brown Avatar answered Nov 15 '22 00:11

Joe Brown