SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to convert date format from one to another?

    Hello,

    I need to somehow convert dates from one format to another.

    The "old" format looks like this:

    04 May 6 7:16
    05 Apr 23 12:42
    05 Jul 3 17:54

    The new format needs to be like this:

    yyyy-mm-dd

    Is this possible? Is there a relatively easy way to do this?
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  2. #2
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look at mktime().

    You should split the time into day month year minute and second (you will need to turn the month into a number) and then you should be able to use mktime:

    PHP Code:
    echo date("Y-m-d"mktime($hour$minute$second$month$day$year)); 
    This sould output the date in the format yyyy-mm-dd

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    or
    PHP Code:
    echo date("Y-m-d",strtotime($time)); 
    Saul

  4. #4
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon
    or
    PHP Code:
    echo date("Y-m-d",strtotime($time)); 

    OK - forget my method php_deamon's way is much better

  5. #5
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon
    or
    PHP Code:
    echo date("Y-m-d",strtotime($time)); 
    It's close, but with the source date of "06 Apr 25 13:36" I end up with a converted date of "2025-04-06".

    I'll see if I can try and split up the original date components or something...
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  6. #6
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I ended up with this:

    $original_date = "06 Apr 25 13:36";
    $pieces = explode(" ", $original_date);
    $new_date = date("Y-m-d",strtotime($pieces[2]." ".$pieces[1]." ".$pieces[0]));

    Thanks folks!
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •