Using Strtotime for Dates Before 1970

Create Date object in PHP for dates before 1970 in certain format

Try this function.

Edit: First we will convert two digit year in 4 digit. Then we will form complete date and pass it to function.

 $original_date = '22-10-49';
$date_part = explode('-',$original_date);

$baseyear = 1900; // range is 1900-2062
$shortyear = $date_part[2];
$year = 100 + $baseyear + ($shortyear - $baseyear) % 100;
$subdate = substr( $original_date, 0, strrpos( $original_date, '-' ) );
$string = $subdate."-".$year;

echo safe_strtotime($string);

function safe_strtotime($string)
{
if(!preg_match("/\d{4}/", $string, $match)) return null; //year must be in YYYY form
$year = intval($match[0]);//converting the year to integer
if($year >= 1970) return date("Y-m-d", strtotime($string));//the year is after 1970 - no problems even for Windows
if(stristr(PHP_OS, "WIN") && !stristr(PHP_OS, "DARWIN")) //OS seems to be Windows, not Unix nor Mac
{
$diff = 1975 - $year;//calculating the difference between 1975 and the year
$new_year = $year + $diff;//year + diff = new_year will be for sure > 1970
$new_date = date("Y-m-d", strtotime(str_replace($year, $new_year, $string)));//replacing the year with the new_year, try strtotime, rendering the date
return str_replace($new_year, $year, $new_date);//returning the date with the correct year
}
return date("Y-m-d", strtotime($string));//do normal strtotime
}

Output: 1949-10-22

Source: Using strtotime for dates before 1970

DateTime not working for dates before 1970

The computer doesn't know whether you mean 2000 or 1900. You can just take the last 2 digits of the year and put "19" before like:

$date = new DateTime("18-Jul-46");
$date->format('d/m/19y');

If you want to use 2000 as well, this code will use the closest number to 1970

$date = new DateTime("18-Jul-20");
$date->format('d/m/Y');
$t1 = $date->format('19y');
$t2 = $date->format('20y');

if(abs(1970-$t1)<abs(1970-$t2))
echo $t1; //Take the 19.. one
else
echo $t2; //Take the 20.. one

But in the end, you can't be sure that even 2030 would be correct.

PHP strtotime(): date showing '1970-01-01 ' after conversion

Your format is not a format that the parser understands.

In your case 13 is not a "month". So the parser doesn't understand to date.

You should use DateTime::createFromFormat():

$date = DateTime::createFromFormat('m-d-Y H:i:s','04-13-2018 0:00:53');
echo $date->format('Y-m-d H:i:s');

Output:

2018-04-13 00:00:53

Note that the format could also be: 'm-d-Y G:i:s' with G for "24-hour format of an hour without leading zeros".

php strtotime turn 1970

strtotime in conjunction with date seems to work OK for the given string

$str='11 Jun 2017';
echo date('d.m.Y',strtotime( $str ) );

outputs

11.06.2017

strtotime() is returning a 1970 date and don't comparing well to time()

Please check below code:

$requestOrder->date="24/12/2021 00:00:00";
$date=str_replace("/",'-',$requestOrder->date);
if(strtotime($date)>time())
{
//this will execute as request date is greater than current date
}
if(strtotime($date)<time())
{
//this will not execute as current date less than request date
}

PHP - strtotime() return 1970

This is due to localisation. Try giving a different format, as the format matters a lot:

echo date("d.m.Y-H:i:s", strtotime("01/01/2000 11:12:32"));
echo date("d.m.Y-H:i:s", strtotime("01-01-2000 11:12:32"));
  1. You should not have . for date and month separator.
  2. You cannot separate date and time using -.

If you are getting the input from another source, try using str_replace:

echo date("d.m.Y-H:i:s", strtotime(str_replace(array(".", "-"), array("/", " "), "01.01.2000-11:12:32")));

Output: http://ideone.com/d19ATK

php - using strtotime date goes to 1970s

The string 2016 06 22 is not a valid date format according to the manual. Try to add hyphens:

$date_init = date('Y m d', strtotime('next monday', strtotime('2016-06-22')));

You can find all valid date formats here: http://php.net/manual/en/datetime.formats.date.php

PHP date issue, can't convert date before 1970-01-01

If your PHP version allow it consider Using DateTime instead of strtotime :

$date = DateTime::createFromFormat('d M Y','17 Jan 1900');
echo $date->format('Y-m-d');

For PHP version between >= 5.2 and <= 5.3 simply use the DateTime constructor :

$date = new DateTime('17 Jan 1900');
echo $date->format('Y-m-d');


Related Topics



Leave a reply



Submit