i get a epoch time returned from a webservice wich is about 3 years off in PHP but fine in javascript and the epochconverter.com
JS:
alert('book '+ new Date(1285565357893)); // returns a time this morning 27 sep 2010, Correct!
PHP:
echo strftime('%x', 1285565357893); // returns a date in 2013, Wrong !
Timezone is set to: Europe/Amsterdam
What am i doing wrong here ?
OK, some simple time basics for you.
Javascript Date class... when you pass a numeric value to the constructor, this is the number of milliseconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
PHP date is measured as the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Convert from milliseconds to seconds in PHP by dividing by 1000.
echo strftime('%x', floor(1285565357893/1000));
Related
Currently I am implementing one of API.Which returns date as 1389914586 .I don't know which type of date format it is? Whether it is offset format or any other
It's the number of seconds since midnight Jan 1, 1970, UTC.
You can see what it is displaying by using PHP's date format.
<?php
print date("Y-m-d H:i:s", 1389914586);
That will output:
2014-01-16 16:23:06
This is called unix time, calculated as the number of seconds from Thursday, 1 January 1970.
This question already has answers here:
why strotime returns negative value in php?
(6 answers)
Closed 9 years ago.
Having Europe/Amsterdam as timezone.
Why does:
strtotime("January 1, 1970 00:40:00');
returns
-1200
I don't get it.
Because, php counts time starting from January 1, 1970 00:00:00 . So you are back from the reference time, That's the possible reason. So 1200 would be
( - 60*40 (+/-) time_difference_in_your_timezone)
The strtotime function parses the strinf date into "into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)". Please see the php documentation.
The -1200 is caused by your time zone.
strtotime is calculating from January 1, 1970 00:00:00 UTC. If you calculate the date back from UTC+1 to UTC you are at December 31, 1969 23:40:00 UTC.
I am using strtotime to convert a date to a unixtime stamp.
Year, date and day comes as different values to code and I am using the below code to produce the timestamp.
$year = '1961';
$month = '2';
$day = '15';
$date = $year."-".$month."-".$day;
echo strtotime($date);
The above code prints : -27648000 for me. If the year is above 1970 it prints out positive results. I am still learning with the timestamp, if any one can help me out. The main aim is to convert a date to unix timestamp.
The questions is why it gives negative results, Am I coding it bad!? I am also tried mktime, but still the same result.
Thanks,
Tanmay
It's to do with the Unix Epoch.
See: date() and time()
The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems
That's the expected behavior.
strtotime returns a UNIX timestamp, the number of seconds since Jan 1 1970 (not considering leap seconds). For dates before that, it will return a negative number.
Unix time starts at the Unix epoch which is Midnight on Jan 1, 1970. So any date before that will result in a negative value being returned.
This is a Unix timestamp. The Unix/PHP base date is January 1, 1970 at 00:00 UST, and the timestamp is measured in seconds. If negative, it is the number of seconds before the base date; if positive, the number of seconds after
I am still learning with the timestamp,
google unix timestamp -> http://en.wikipedia.org/wiki/Unix_time
defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of
January 1, 1970
http://en.wikipedia.org/wiki/Unix_time
Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds. It is used widely, not only in Unix-like operating systems, but also in many other computing systems and file formats. It is neither a linear representation of time nor a true representation of UTC (though it is frequently mistaken for both), as the times it represents are UTC but do not represent standard UTC leap seconds (e.g. December 31, 1998 23:59:60)...
Times before 1/1/1970 are negative values as they occured before the start of UTC.
Every time in PHP when I make a variable such as this one:
$date = strtotime($row['date']);
$date = date("M d Y \a\\t g:i A", $date); // Mmm dd YYYY at h:mm PM/AM
and somehow row['date'] happens to be 0, the date Dec 31 1969 at 7:00 PM is displayed on the screen? Google does not tell me much, I was wondering if this date had any significances.
The Unix epoch is the time 00:00:00 UTC on 1 January 1970. This is the reference point for all time stamps. When you use PHP's date/time functions, you're always working with the number of seconds since the epoch. Time 0 is the epoch, and you (or your web server) must be on the east coast of the US, which is 5 hours behind UTC time.
I find it funny that not a single response here attempted to answer your actual question, which was (if I can paraphrase) "What is the significance of the actual date of Unix epoch time"?
I'm not an expert on the subject but basically, as I understand it, the concept of epoch time was invented in 1971. The programmers chose the arbitrary date of January 1, 1971 GMT to be epoch time. This was partly due to the fact that older computers couldn't handle large numbers so the date had to be in the recent past. Afterwards, epoch time was adjusted to be Jan 1, 1970 so as to be a nice, round number.
So basically, nothing "happened" on that date. It was an arbitrary date chosen based on the original time of the work being done.
Unix timestamps are measured in "time since the Unix Epoch", which is Midnight GMT at the end of Dec. 31 1969 (a.k.a. 00:00 GMT Jan 1 1970). Since you appear to be on Eastern Standard Time, which is GMT-5, you get 7pm Dec. 31st 1969 for a unix timestamp value of 0.
Let me guess: you live on the east coast of the USA?
PHP, like many other systems uses the Unix epoch to measure time, i.e. a value of 0 represents January 1, 1970, midnight UTC - which is the same as Dec 31 1969 at 7:00 PM Eastern Standard Time.
One format in which date objects are stored is the time in seconds that have elapsed from an arbitrary start time. Asking for a formatted version of "0" is like asking for that arbitrary start time. I don't remember why that date was chosen, but I'm sure Wikipedia does. See the article on Unix time below.
Read about Unix Time
what is exactly happing when creating a Unix Timestamp from a given date.
I want the step by step method to create timestamp. (not using php built in functions).
EDIT: What is the specialty of this date :) January 1st 1970 00:00 h UTC
January 1st 1970 00:00 h UTC is the date of the Epoch :
The "epoch" then serves as a reference
point from which time is measured.
Time measurement units are counted
from the epoch so that the date and
time of events can be specified
unambiguously.
In computing, this date is the Epoch for UNIX time : UNIX timestamps (as used by PHP) are actually the number of seconds since that Epoch.
This means that, to create a timestamp, you have to count how many seconds have passed since the 1st January 1970.
You just have to count the seconds that elapsed from January 1st 1970 00:00 h UTC until now (not counting leap seconds)!