6 Replies - 240 Views - Last Post: 11 March 2014 - 05:46 PM Rate Topic: -----

#1 UnknownCodester  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 05-March 13

Converting Date in text to numeric date

Posted 11 March 2014 - 08:06 AM

Hi,

I am trying to convert a date from one format to another like these examples below;

Monday 10th July 2014 -----------> 2014:07:10
Tuesday 11th July 2014 ----------> 2014:07:11

This is because my mysql database is in the yyyy:mm:dd format..

What is the best way to do this sort of thing?

I am currently thinking;

- Some sort of regular expression to "parse?" the data

Is This A Good Question/Topic? 0
  • +

Replies To: Converting Date in text to numeric date

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3476
  • View blog
  • Posts: 10,009
  • Joined: 08-June 10

Re: Converting Date in text to numeric date

Posted 11 March 2014 - 08:16 AM

View PostUnknownCodester, on 11 March 2014 - 04:06 PM, said:

What is the best way to do this sort of thing?

not at all. MySQLs native date format is YYYY-MM-DD and without a proper date type for your fields you cannot do date calculation in SQL (e.g. like get all results from Feb 2013).

other than that, PHPs strtotime() function is the primary date parser.
Was This Post Helpful? 0
  • +
  • -

#3 UnknownCodester  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 05-March 13

Re: Converting Date in text to numeric date

Posted 11 March 2014 - 10:45 AM

Okay so I have tried this code below;
<?php

$date = "Tuesday 10 September 2000";

$newdate =  date('d/m/Y', strtotime($date));

echo $newdate;


?>


but the output is 12/09/2000 and not 10/09/2000?

Does anyone know why this is? (it seems that it's added the day of week to the date of month but I don't know why?)
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3476
  • View blog
  • Posts: 10,009
  • Joined: 08-June 10

Re: Converting Date in text to numeric date

Posted 11 March 2014 - 11:02 AM

thats because September 10, 2000 was a Sunday, not a Tuesday.
Was This Post Helpful? 2
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6031
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: Converting Date in text to numeric date

Posted 11 March 2014 - 11:03 AM

Never mind I totally missed it!

This post has been edited by JackOfAllTrades: 11 March 2014 - 11:04 AM

Was This Post Helpful? 0
  • +
  • -

#6 UnknownCodester  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 05-March 13

Re: Converting Date in text to numeric date

Posted 11 March 2014 - 11:26 AM

Haha woops, I was just putting in random dates to test it. So that now works when I put a correct date in, Thank you.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,000
  • Joined: 08-August 08

Re: Converting Date in text to numeric date

Posted 11 March 2014 - 05:46 PM

Note that MySQL uses the Year first but you have it last.

MySQL: YYYY-MM-DD
You: d/m/Y
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1