Jump to content

A bit of help to a newbie - date formating


Tiruak

Recommended Posts

Hello ladies and gals

I'm very new to php, only a couple months working with it, and am having some difficult to do a couple of things.

 

My question is probably very stupid, and there are probably some obvious answers to it, but I really appreciate if you can help me get to the solution, even if you have to call me "dumb" after you do it :)

 

After completing a form, an user has a field to insert a date of birth, in a format dd/mm/yyyy (eg 23/06/1980). When I try to add this to the database, in a field that expects a date, it actually just gives me 0000-00-00 as a result. Now, I understand that the sql database is expecting the date to be passed in a different format, so I tried to use the following command on my date:

$date="23/06/1980";  // this variable is just for testing purpose, I actually get the date with $_POST
$new_date=date('Y-m-d',strtotime($date));

 

Now if I echo $new_date, it returns me 1980-23-06, because the strtotime() function expects a date in US English format (m/d/Y).

 

So, I assume the answer is probably simple, but I can't get to format the date in the way I need, to be able to add it to the database and store it correctly. Of course, when I read it back and format to the user to read, I will need to reverse the process, but that should be easy to do once someone helps me to format it to add to the database.

 

Thanks for any help, and I'm sorry if this has been asked a thousand times.

Link to comment
Share on other sites

You have to convert it to a Y-m-d format before inserting it into the database:

<?php
$date="23/06/1980";
list($d,$m,$y) = explode('/',$date);
$new_date = "$y-$m-$d";
echo $new_date;
?>

 

Ken

 

Thanks alot Ken,

This solves the problem and also teaches me a new function that may be very helpful :)

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.