PHP date showing '1970-01-01 ' after conversion
Asked Answered
S

8

36

I have a form in which date format is dd/mm/yyyy . For searching database , I hanverted the date format to yyyy-mm-dd . But when I echo it, it showing 1970-01-01 . The PHP code is below:

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

Why is it happening? How can I format it to yyyy-mm-dd?

Sidonnie answered 24/1, 2012 at 9:38 Comment(4)
because its 0000-00-00 in your dbHoyt
its not coming from db, its from my html page to phpSidonnie
The strtotime input is wrong. Check the $date1.Zacarias
@Sidonnie - The format dd/mm/yyyy should be dd-mm-yyyy so you will need to replace your / characters.Myeloid
M
87

Replace / with -:

$date1 = strtr($_REQUEST['date'], '/', '-');
echo date('Y-m-d', strtotime($date1));
Myeloid answered 24/1, 2012 at 9:40 Comment(4)
Simple and effective @Cyclone!Hazaki
I had this probem when coverting two UK dates to MySQL format. Your help is very much appreciated. Works like a dream!Outtalk
I do the same but problem is still can you help @MyeloidLynnelynnea
@ankitsuthar - Very hard to give a solution without seeing the date you're trying to format. Try removing any whitespaces from the string before using strtotime()?Myeloid
Y
36

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.

Yardman answered 24/1, 2012 at 9:43 Comment(1)
Thus, putting a condition likeif (strtotime($date1)) before the conversion seems like a good practice.Emission
W
2
$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

Withindoors answered 24/10, 2018 at 12:12 Comment(0)
C
1
$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.

Contiguous answered 16/10, 2017 at 16:46 Comment(1)
I've used this with PHPExcel and it works perfect. Thank you!Porty
A
0

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;
Adams answered 12/12, 2018 at 6:12 Comment(0)
H
0

The issue is when your data is set to 000-00-00 or empty you must double-check and give the correct information and this issue will go away. I hope this helps.

Harquebusier answered 11/2, 2020 at 21:4 Comment(0)
L
-1

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');
Lockyer answered 29/4, 2017 at 6:34 Comment(1)
I don't see a difference between those twoEnthrall
B
-2

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

date('d/m/Y', strtotime(str_replace('.', '-', $row['DMT_DATE_DOCUMENT'])));
Beefeater answered 19/8, 2015 at 6:29 Comment(1)
What kind of answer is this, you picked code from everyone else and put it on one line? Have you ever heard about code readability?Myeloid

© 2022 - 2024 — McMap. All rights reserved.