Jump to content

Need help with function date() and strtotime()


JuanKiller69

Recommended Posts

Hello guys! this is my first post, hope I can have some help soon...

 

In a CSV file I got these fields with dates that look like this:

 

... , ... , ... , ... , ... ,09/05/2011,15/05/2011,

 

I'm gonna put them in a MySQL table, where the fields are DATE TYPE, BUT if I put them as a normal string, like this:

 

(Let's asume that I have all that funny CSV data inside an array)

 

INSERT INTO thetable VALUES("...", "...", "$values_array[thekey]", "$values_array[$theotherkey]", ..., ...);

 

it will show me 00-00-0000!!! :S

 

So, I'm using this functuin strtotime to fix it: ($record is the name of the actual array, dont worry about it)

 

$date1 = $record[8];

$date1 = date("Y-m-d", strtotime($date1));

$date2 = $record[9];

$date2 = date("Y-m-d", strtotime($date2));

 

Now, I use this query:

 

$query = "INSERT INTO thetable VALUES ('', '', '', '$date1', '$date2', '', '', '', '', '', '', '')";

 

and it shows me the date1 correctly, like: 2011-09-05, BUT The date2 is always wrong! even when the string inserted ($record[9]) is 2011/15/05, the date it tries to insert is "1969-12-31" <---- !!!

 

What's wrong, why does it work with the first date and does not with the second one?

 

Thanks for your help beforehand!

Link to comment
Share on other sites

even your first date is incorrect. strtotime is looking for the month in the wrong field, and 15 is an invalid month so you get an incorrect date.

 

you first date is 9th May 2011, but you're inserting 5th September 2011. (i.e. day and month are switched)

 

try splitting it manually:

 

$date1 = explode("/",$record[8]);
$realdate1 = $date1[2].'-'.$date1[1].'-'.$date1[0];

$date2 = explode("/",$record[9]);
$realdate2 = $date2[2].'-'.$date2[1].'-'.$date2[0];

 

 

Link to comment
Share on other sites

If you're using PHP 5.3, then use DateTime::createFromFormat() (if you're not, why not?!).

 

$date       = '15/05/2011';
$datetime   = DateTime::createFromFormat('d/m/Y', $date);
$mysql_date = $datetime->format('Y-m-d');

 

If you want to stick with strtotime() then formats that it will accept are detailed under Date Formats.

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.