PHP date showing '1970-01-01 ' after conversion

◇◆丶佛笑我妖孽 提交于 2019-11-27 18:11:34

Replace / with -:

$date1 = strtr($_REQUEST['date'], '/', '-');
echo date('Y-m-d', strtotime($date1));

January 1, 1970 is the so called Unix epoch. It's the date where they started counting the Unix time. If you get this date as a return value, it usually means that the conversion of your date to the Unix timestamp returned a (near-) zero result. So the date conversion doesn't succeed. Most likely because it receives a wrong input.

In other words, your strtotime($date1) returns 0, meaning that $date1 is passed in an unsupported format for the strtotime function.

$inputDate = '07/05/-0001';
$dateStrVal = strtotime($inputDate);
if(empty($dateStrVal))
{
  echo 'Given date is wrong'; 
}
else{
 echo 'Date is correct';
}

O/P : Given date is wrong

Use below code for php 5.3+:

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

Use below code for php 5.2:

$date = new DateTime('1900-02-15');
echo $date->format('Y-m-d');
Tye Lucas
$date1 = $_REQUEST['date'];

if($date1) {
    $date1 = date( 'Y-m-d', strtotime($date1));
} else {
    $date1 = '';
}

This will display properly when there is a valid date() in $date and display nothing if not.
Solved the issue for me.

Another workaround:

Convert datepicker dd/mm/yyyy to yyyy-mm-dd

$startDate = trim($_POST['startDate']);
$startDateArray = explode('/',$startDate);
$mysqlStartDate = $startDateArray[2]."-".$startDateArray[1]."-".$startDateArray[0];
$startDate = $mysqlStartDate;

finally i have found a one line code to solve this problem

date('d/m/Y', strtotime(str_replace('.', '-', $row['DMT_DATE_DOCUMENT'])));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!