Jump to content

Date Format Issue


MoFish

Recommended Posts

Hi,

 

I have a two input fields posting dates in the format of 01/11/2013 (d/m/Y)

 

I am trying to change these into the database format of 2013-11-01 (Y-m-d)

 

I'm getting unexpected results for some reason and am not sure the reason behind it.

 

Could anyone clarify whats going on?

	if(isset($_POST['filterBtn'])){
		$postfromdate = $_POST['fromDate']; // 26/11/2013		
		$fromdate = date('Y-m-d', strtotime($postfromdate));

		echo "START <br/> post value <strong>" . $postfromdate . "</strong> converted value <strong>". $fromdate . "</strong> <br/>";
		
		$posttodate = $_POST['toDate']; // 26/11/2013
		$todate = date('Y-m-d', strtotime($posttodate));
		
		echo "END <br/> post value <strong>" . $posttodate . "</strong> converted value <strong>". $todate . "</strong> <br/>";
        }

and the results:

 

START
post value 01/11/2013 converted value 2013-01-11
END
post value 30/11/2013 converted value 1969-12-31

 

I'm not sure why im getting 1969 in some cases.

 

Thanks,

 

MoFish

 

Link to comment
https://forums.phpfreaks.com/topic/284307-date-format-issue/
Share on other sites

Some formats don't work with strtotime() and d/m/y is one of them.

 

A couple of options


$date = str_replace('/', '-', '26/11/2013');
echo date('Y-m-d', strtotime($date)); //--> 2013-11-26
$date = DateTime::createFromFormat('d/m/Y', '26/11/2013');
echo $date->format('Y-m-d');  //--> 2013-11-26
Link to comment
https://forums.phpfreaks.com/topic/284307-date-format-issue/#findComment-1460248
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.