why strotime returns negative value in php? - php

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.

Related

Timestamp before 1970 is saved as 0000-00-00 00:00:00

I have a timestamp column corresponding to model property.
But when I try to save a timestamp before 1970 in the table appears the value of 0000-00-00 00:00:00.
Am I not using the appropriate format? Is date more suitable? Is there a workaround so I don't need to change my schema?
The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).
If you're planning on using times that fall previous to that date, I recommend using a date field.
Quoting from the MySQL docs:
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
That is a real problem, but there is a nice work-around.
We know that concerning timestamps, the absolute 0 is in 1970. So, why do not we change our representation model and consider January 1st of 1970 to be the day Jesus has been born, namely January 1st of 1 AD. If you do that, then the year of 2000 would be represented by the number of 3970 (2000 + 1970). You could store dates like that and convert them when you view them or when you enter them.

Which type of date format it is?

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.

Epoch to date is 3 years off

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));

What happened on Dec 31 1969 at 7:00 PM

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

php Timestamp working logic

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)!

Categories