mattichu Posted October 14, 2012 Share Posted October 14, 2012 (edited) Hi, Im posting a date in the format : dd/mm/yyyy and am wanting to store it in a database as a string. I'm using the following code: $dayoff=$_POST['dayoff']; $dayoffst=strtotime('$dayoff'); $date=date('dS M Y',$dayoffst); However when I echo the $dayoffst it doesn't return anything any ideas? Edited October 14, 2012 by mattichu Quote Link to comment https://forums.phpfreaks.com/topic/269457-basic-problem/ Share on other sites More sharing options...
Mahngiel Posted October 14, 2012 Share Posted October 14, 2012 Besides single quotes not being the correct way to parse PHP variables, there's no reason to quote $dayoffst=strtotime('$dayoff'); Quote Link to comment https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385161 Share on other sites More sharing options...
Pikachu2000 Posted October 14, 2012 Share Posted October 14, 2012 You should store dates in the proper YYYY-MM-DD format, in the proper field type (DATE) in the database. Quote Link to comment https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385162 Share on other sites More sharing options...
mattichu Posted October 14, 2012 Author Share Posted October 14, 2012 (edited) I've changed the code to: $dayoff=$_POST['dayoff']; $date=date("dS M Y",$dayoff); But this gives: 31st Dec 1969 $dayoff outputs correctly Edited October 14, 2012 by mattichu Quote Link to comment https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385163 Share on other sites More sharing options...
jcbones Posted October 14, 2012 Share Posted October 14, 2012 Is $dayoff a proper unix timestamp? Because, that is what the second parameter of date requires. If you are using a formatted date, then you need to use strtotime on it. However, as has been stated, don't store your date in the database this way, as you will lose all functionality of working with dates in the database. You should just cast the date to your desired output via the correct database functions. Quote Link to comment https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385166 Share on other sites More sharing options...
Barand Posted October 14, 2012 Share Posted October 14, 2012 strtotime does recognise many different date formats, unfortunately for us Brits, dd/mm/yyyy or dd/mm/yy are not amongst them. Quote Link to comment https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385188 Share on other sites More sharing options...
Barand Posted October 15, 2012 Share Posted October 15, 2012 (edited) strtotime() date format examples <?php $dates = array ( '25/12/2012', '25/12/12', '25.12.12', '25-12-12', '25-12-2012', '2012-12-25', '12-12-25', '25-12-2012', '12/25/2012', '25-DEC-12', '25 dec 2012' ); foreach ($dates as $d) { printf('%s -> %s<br />', $d, date('j F Y', strtotime($d))); } ?> output: 25/12/2012 -> 1 January 1970 x 25/12/12 -> 1 January 1970 x 25.12.12 -> 25 December 2012 25-12-12 -> 12 December 2025 x 25-12-2012 -> 25 December 2012 2012-12-25 -> 25 December 2012 12-12-25 -> 25 December 2012 25-12-2012 -> 25 December 2012 12/25/2012 -> 25 December 2012 25-DEC-12 -> 25 December 2012 25 dec 2012 -> 25 December 2012 Edited October 15, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385209 Share on other sites More sharing options...
jcbones Posted October 15, 2012 Share Posted October 15, 2012 Hey, how did I miss the whole first part of the OP. It would accept dd-mm-yyyy or dd.mm.yy. Both of which could be fixed with a simple str_replace. Quote Link to comment https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385211 Share on other sites More sharing options...
DarkerAngel Posted October 15, 2012 Share Posted October 15, 2012 You should store dates in the proper YYYY-MM-DD format, in the proper field type (DATE) in the database. I still just store the unix_timestamp (INT) and format it in PHP although I'm getting some legs in some of the date/time functions of MySQL so I might start working out some "proper" ways of storing date/time in MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385264 Share on other sites More sharing options...
Barand Posted October 15, 2012 Share Posted October 15, 2012 (edited) Hey, how did I miss the whole first part of the OP. It would accept dd-mm-yyyy or dd.mm.yy. Both of which could be fixed with a simple str_replace. As long as you have 4-digit years otherwise confusion reigns dd mm yy(yy) formatted inputs 03.12.12 -> 15 October 2012 ??? 03.12.2012 -> 3 December 2012 03-12-12 -> 12 December 2003 03-12-2012 -> 3 December 2012 Edited October 15, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385276 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.