epoch time conversion - php

how can I convert this epoch time to human readable date time
1331515367
I used the following code but this gives me the current time in my zone
$time = 1331515367
echo date('Y-m-d H:i:s', $time);
thannks

Dates generated on your server are going to be in whatever time zone your server says to use. If you want to change it use: http://php.net/manual/en/datetime.configuration.php
date.timezone ="timezone";
For a list of time zones: http://php.net/manual/en/timezones.php
All of this was found using this google search: php date timezone
https://www.google.com/search?sourceid=chrome&ie=UTF-8&q=php+date+timezone

Related

Unix Time Stamp starts with "1970/01/01 02:00:00" in PHP

I see "1970/01/01 02:00:00" when I run that script:
echo date('Y/m/d H:i:s',0) ;
Why does it start with "02:00:00"??
Because you are in/have set PHP to a UTC+2 timezone. Use date_default_timezone_set to set it to UTC to see UTC time. Or use gmdate instead of date which always outputs times in that timezone.
(Caveat emptor that UTC is not GMT, but close enough for this example.)
Because your local default time zone is not the Greenwich time zone.
date_default_timezone_set("Etc/GMT");
echo date('Y/m/d H:i:s',0);

ISO date formats to wrong day

Short question but I can't get my finger on it. This piece of code:
$date = '2015-12-08T00:00:00+01:00';
echo date('D', strtotime($date));
returns Mon while
$date = '2015-12-08T00:00:00';
echo date('D', strtotime($date));
returns Tue. Why is that? The +01:00 is for the timezone, but that should not affect the day in my opinion.
First I've looked up that 08-12-2015 is in fact a Tuesday, so now we know the first one is incorrect.
PHP's date() is an Unix timestamp according to their own docs.
My belief is that adding the +1 as a timezone triggers the calculation to the +0 timezone (UTC) when asking for the day of the week and therefore returns 23:00 on monday as the current UTC time.
You can specify the timezone before executing the rest of the code: date_default_timezone_set('Europe/Amsterdam');
<?php
date_default_timezone_set('Europe/Amsterdam'); //this is an example of a +1 timezone, choose one from http://php.net/manual/en/timezones.php
$date = '2015-12-08T00:00:00+01:00';
echo date('D', strtotime($date) );
?>
strtotime will parse your date string using the supplied time zone or using the default timezone if unspecified. We can't see from the code you've posted what time zone your server is configured to, but once the date is parsed and converted to your time zone, the time may legitimately occur in the previous day, hence why you're seeing 'Mon'.
Either supply a time zone in the strtotime call via the now argument or set one globally with date_default_timezone_set.

PHP date timestamp timezone not converted properly

So I have this code:
$timestamp = 1414708099;
echo $timestamp;
$date = date_make_date($timestamp, 'UTC', 'datestamp');
date_timezone_set($date, timezone_open('America/New_York'));
$timestamp = $date->format('U');
echo '<br>';
echo $timestamp;
which is supposed to convert the timezone of the initial timestamp from UTC to new york.
but then this ends up printing
1414708099<br>1414708099
hence the timezone didnt change...
what did I do wrong?
btw it also uses Drupal 6 date_api.module: http://drupalcontrib.org/api/drupal/contributions!date!date_api.module/function/date_make_date/6
As per comments
A timestamp is always UTC. You can't apply a time zone to a timestamp - consider its timezone as 0. Whatever you do, it stays 0. You asked for a date formatted with U - manual states this:
U: Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
You can't get seconds from Unix Epoch for New York. That number is the same for any location in the world.
Now, had you formatted that date using, say, $date->format('Y-m-d H:i:s') then you would get correctly formatted time with the timezone offset for New York.
Long story short - there is no problem whatsoever here. It all works as intended.

DateTime->format("U") doesn't return a value with timezone info

In PHP 5.2, I'm using the following code to get a timestamp from a DateTime object.
$dateTime = new DateTime("now", new DateTimeZone("America/Los_Angeles") );
echo $dateTime->format("U");
the problem is that format("U") simply returns server timestamp, which is UTC.
How do I make it to return a timestamp from Pacific Time Zone (Los Angeles) ?
Your concept for timestamp is not right, timestamp is timezone independent, it is defined as the number of seconds that have elapsed since midnight Coordinated Universal Time (UTC), 1 January 1970.
Try setting timezone at top of PHP script. I think timestamps are always UTC. Use date() function to format it into what you need.
// set timezone to pacific time
date_default_timezone_set('America/Los_Angeles');

Change the simple date time format to show time zone

I have a simple date time in the format 10/20 4:30PM. I want it to display in the format
10/24 1:30PM -007. I am saying date('Y-m-d H:i:sT', $time_recieved) assuming T is for timezone. But It is still not giving the time zone. What might be the reason? Is there any other format I am missing?
What you want is O not T I believe.
Check the PHP date page for all the format listings possible.
http://php.net/manual/en/function.date.php
You can have a look at the official manual of PHP : date ();
http://www.php.net/manual/en/function.date.php
I tested that and I used on my server with :
$time = time();
echo date('Y-m-d H:i:s T', $time);
it shows the timezone perfectly. Check $time_received, it might be an erroneous number, try with local time with timeĀ“() function to ensure that it's OK.
Good luck

Categories