MoFish Posted November 26, 2013 Share Posted November 26, 2013 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: STARTpost value 01/11/2013 converted value 2013-01-11ENDpost value 30/11/2013 converted value 1969-12-31 I'm not sure why im getting 1969 in some cases. Thanks, MoFish Quote Link to comment https://forums.phpfreaks.com/topic/284307-date-format-issue/ Share on other sites More sharing options...
Solution Barand Posted November 26, 2013 Solution Share Posted November 26, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/284307-date-format-issue/#findComment-1460248 Share on other sites More sharing options...
MoFish Posted November 26, 2013 Author Share Posted November 26, 2013 Thank you Barand - This has resolved my issue. Quote Link to comment https://forums.phpfreaks.com/topic/284307-date-format-issue/#findComment-1460249 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.