Jump to content

Date Format Issue


MoFish
Go to solution Solved by Barand,

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
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.