开发者

PHP DATE strtotime date "0001-01-01" [duplicate]

开发者 https://www.devze.com 2023-04-10 09:52 出处:网络
This question already has answers here: Using strtotime for dates before 1970 (7 answers开发者_如何学运维)
This question already has answers here: Using strtotime for dates before 1970 (7 answers开发者_如何学运维) Closed 7 years ago.

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.


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


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


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


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号