-1

Hello guys is there a way to convert the date 0001-01-1 to a time format? I am trying to to use the php function strtotime("0001-01-01"), but it returns false.

My goal is to count the days from the date: 0001-01-01 to: 2011-01-01.

Leonid Glanz
  • 1,261
  • 2
  • 16
  • 36
rechie
  • 2,139
  • 5
  • 25
  • 38
  • `strtotime()` converts date to UNIX timestamp, which is a number of seconds since January 1 1970 00:00:00 UTC - that's why it fails on such dates. – binaryLV Oct 03 '11 at 07:45
  • 1
    http://stackoverflow.com/questions/2871264/using-strtotime-for-dates-before-1970 – Marek Sebera Oct 03 '11 at 07:45
  • 3
    Trying to use dates before 1600 or so is unreliable. – Ignacio Vazquez-Abrams Oct 03 '11 at 07:52
  • I found my solution by this means; I calculated the in C# between date "0001-01-01" to "2000-01-01" and the result is 730119. So my code goes this way //number of days = days from START_DATE to 2000, 01, 01 then add 730119 $count = ((strtotime("2011-01-01") - strtotime("2000-01-01")) / 86400) + 730119; – rechie Oct 03 '11 at 07:59
  • 1
    @rechie, nice workaround, if you have a limited set of "start dates", i.e., if "starte date" fits in "Unix epoch". – binaryLV Oct 03 '11 at 08:02
  • 1
    I would be very careful about using year 0 or year 1 as an epoch. I'm not entirely sure what calendar C# uses for dates before ~1600, but it almost certainly is going to be wrong. If you must represent day numbers as integers, pick a recent date for which there is no confusion (e.g. 2000-01-01) or use something like the Julian Day Number. – tc. Oct 03 '11 at 23:19

6 Answers6

1

Use DateTime class. strtotime returns a timestamp which in your case will be out of int bounds.

Mārtiņš Briedis
  • 17,396
  • 5
  • 54
  • 76
  • 2
    It wouldn't be on a 64-bit system, and DateTime has the exact same restrictions – Evert Oct 03 '11 at 07:47
  • Have to agree with @Evert about `DateTime` - it gave only 16 years difference between year 1 and year 2011. As for relying on 64bit systems - I would not do that... – binaryLV Oct 03 '11 at 07:58
  • @Evert Ah, thanks, that should explain why it's working on some systems, and not on other systems, though the same PHP version number. – nl-x Jan 18 '18 at 10:48
1

As Ignacio Vazquez-Abrams has commented, dates before the adoption of the Gregorian calendar are going to be problematic:

  • The Gregorian calendar was adopted at different times in different countries (anywhere from 1582 to 1929). According to Wikipedia, a few locales still use the Julian calendar.
  • Days needed to be "removed" to switch to the Gregorian calendar, and the exact "missing" dates differ between countries (e.g. the missing days in September 1752). Some countries tried to do a gradual change (by removing February 29 for a few years); Sweden switched back to the Julian calendar to "avoid confusion", resulting in a year with February 30.
  • Years were not always counted from January 1 (which has left its legacy in things like the UK tax year).

Michael Portwood's post The Amazing Disappearing Days gives a reasonable summary.

In short, you have to know precisely what you mean by "1 Jan 1 AD" for your question to make any sense. Astronomers use the Julian Day Number (not entirely related to the Julian calendar) to avoid this problem.

EDIT: You even have to be careful when things claim to use the "proleptic Gregorian calendar". This is supposed to be the Gregorian calendar extended backwards, but on Symbian, years before 1600 observe the old leap year rule (i.e. Symbian's "year 1200" is not a leap year, where in the proleptic Gregorian calendar it is).

Community
  • 1
  • 1
tc.
  • 33,468
  • 5
  • 78
  • 96
0

I am afraid it wont work since strtotime changes string to timestamp, which have lowest value of 0, and its at 1970-01-01.. cant go lower than that..

date('Y-m-d', 0);
Vyriel
  • 199
  • 3
0

From the manual:

If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).

You may want to give 'DateTime::createFromFormat' a shot instead this.

Also note that you cannot go below 1901 on a 32-bit version of PHP.

ann
  • 576
  • 1
  • 10
  • 19
Evert
  • 93,428
  • 18
  • 118
  • 189
0

You cannot do that this way. Here is one option how to do this.

$date1= "2011-01-01";
$date2 = "2011-10-03";

//calculate days between dates

$time_difference = strtotime($date2) - strtotime($date1); 

now you got time difference in seconds from wich you can get days, hours, minutes, seconds.

Janis Lankovskis
  • 1,042
  • 9
  • 9
-1

You can't use the function strtotime because this function return a timestamp and the time stamp start from 1970 so i advise you to searsh for a different way

Rjaibi Mejdi
  • 6,820
  • 3
  • 21
  • 26