PHP-Nut Posted November 3, 2007 Share Posted November 3, 2007 I wonder if anyone could help me, i recently wrote an appliacation for work, in PHP. I have a form that captures customer details and a delivery date field. I used a pull down menu and filled this date field with the next 14 days.. capturing the date as text for example 'Wednesday - 21st - Nov 2007' and storing this in a mysql database, whereby quite a lot of pages pull data out using this date format by comparing dates again accessed from pull down menus.. But what i would like to do is make it more flexable and have a date picker so any date in the future could be added, not just within the next 14 days. But how do i go about changing the date given by the datepicker usually something like '21/11/2007' To The date format i store in my database? Im sure there is a really easy way to do this in PHP? Please Help Me Paul. Quote Link to comment https://forums.phpfreaks.com/topic/75915-help-me-im-out-of-date/ Share on other sites More sharing options...
severndigital Posted November 3, 2007 Share Posted November 3, 2007 i've had to deal with this problem several times so I wrote a function to handle it. $datetoparse = '21/11/2007'; function parseDate($datetoparse){ $edate = explode("/",$datetoparse); //explodes the date into sections $month = $e[1]; $day = $e[0]; $year = $e[2]; //now put it back together in the format you want. $result = "$year-$month-$day"; return $result; } the function should return this .. 2007-11-21 That should do it, you can switch around result to accommodate your format in the database. you can add some validators if you want like .. if($month is > 12){ echo 'some error code here'; } This would keep them from inputing the date the in wrong format. Quote Link to comment https://forums.phpfreaks.com/topic/75915-help-me-im-out-of-date/#findComment-384234 Share on other sites More sharing options...
PHP-Nut Posted November 3, 2007 Author Share Posted November 3, 2007 Hi Thanks For that, But Is there a way to return the timestamp so i can use the date function? To Make it easier on the Screen Operator ive included the Day/month etc ie. 'Wednesday - 21st - Nov 2007' <-------------- This is what my dateofdelivery field stores so i need to go from 21/11/2007 to the above Any Ideas? Thanks for the help so far! Paul. Quote Link to comment https://forums.phpfreaks.com/topic/75915-help-me-im-out-of-date/#findComment-384243 Share on other sites More sharing options...
rajivgonsalves Posted November 3, 2007 Share Posted November 3, 2007 here you go <?php $str = 'Wednesday - 21st - Nov 2007'; $str = date("Y-m-d",strtotime(preg_replace("/^(\w)+|\-| /", "", $str))); echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75915-help-me-im-out-of-date/#findComment-384246 Share on other sites More sharing options...
Psycho Posted November 3, 2007 Share Posted November 3, 2007 I think you are taking the wrong approach by storing the date in the DB in the format 'Wednesday - 21st - Nov 2007'. The 'date' field type in the DB is there for a reason. You can do calculations using a timestamp, but not on a text field. I would suggest using a date timestamp in the database, but using a function to present the date to the user in a friendly format. Quote Link to comment https://forums.phpfreaks.com/topic/75915-help-me-im-out-of-date/#findComment-384254 Share on other sites More sharing options...
PHP-Nut Posted November 3, 2007 Author Share Posted November 3, 2007 Hi Again, I need to go the other way around.. the date picker is going to supply the date as 21/11/2007 then i need to change that to 'Wednesday - 21st - Nov 2007' ready to be stored in the database.. Once Again Thanks For Your Help Paul. Quote Link to comment https://forums.phpfreaks.com/topic/75915-help-me-im-out-of-date/#findComment-384257 Share on other sites More sharing options...
PHP-Nut Posted November 3, 2007 Author Share Posted November 3, 2007 I think you are taking the wrong approach by storing the date in the DB in the format 'Wednesday - 21st - Nov 2007'. The 'date' field type in the DB is there for a reason. You can do calculations using a timestamp, but not on a text field. I would suggest using a date timestamp in the database, but using a function to present the date to the user in a friendly format. I agree, But i don`t need to do any calculations, It is purly Text.. No Different than Say storing their telephone number.. or Postcode.. It is just used to pull out data... Quote Link to comment https://forums.phpfreaks.com/topic/75915-help-me-im-out-of-date/#findComment-384261 Share on other sites More sharing options...
rajivgonsalves Posted November 3, 2007 Share Posted November 3, 2007 here you go <?php $str = 'Wednesday - 21st - Nov 2007'; $str = date("d/m/Y",strtotime(preg_replace("/^(\w)+|\-| /", "", $str))); echo $str; ?> read the date function on php.net http://php.net/date Quote Link to comment https://forums.phpfreaks.com/topic/75915-help-me-im-out-of-date/#findComment-384262 Share on other sites More sharing options...
PHP-Nut Posted November 3, 2007 Author Share Posted November 3, 2007 Hi, I know how to use the Date Function.. I just dont know how to get a timestamp from 21/11/2007? ??? ??? ??? Paul. Quote Link to comment https://forums.phpfreaks.com/topic/75915-help-me-im-out-of-date/#findComment-384275 Share on other sites More sharing options...
rajivgonsalves Posted November 3, 2007 Share Posted November 3, 2007 Piece of cake here you go <?php $date = '21/11/2007'; $time = strtotime(substr($date,-4)."-".substr($date,3,2)."-".substr($date,0,2)); print date("Y-m-d",$time); ?> Quote Link to comment https://forums.phpfreaks.com/topic/75915-help-me-im-out-of-date/#findComment-384281 Share on other sites More sharing options...
PHP-Nut Posted November 3, 2007 Author Share Posted November 3, 2007 Thanks... Your A Star! Quote Link to comment https://forums.phpfreaks.com/topic/75915-help-me-im-out-of-date/#findComment-384288 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.