Jump to content

Help Me - Im Out of Date!


PHP-Nut

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

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.