SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP convert date to MySQL format

    Hi Guys,

    How do I convert a date in mmddyy format (ie: 251207) into a MySQL timestamp in following format: 2008-12-25 00:00:00

    The main thing is that the date gets converted like above, I am not concerned about the time.

    Thanks in advance.

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    str_split

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wonshikee View Post
    str_split
    Unfortunately I don't have PHP 5 installed

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    explode
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Zaggs View Post
    Hi Guys,

    How do I convert a date in mmddyy format (ie: 251207) into a MySQL timestamp in following format: 2008-12-25 00:00:00

    The main thing is that the date gets converted like above, I am not concerned about the time.

    Thanks in advance.
    If you are certain the date will always be in that format you could do -
    PHP Code:
    <?php
    $dte    
    '251208';

    $day    $dte[0].$dte[1];
    $mnth   $dte[2].$dte[3];
    $yr     $dte[4].$dte[5];

    printf("20%s-%s-%s 00:00:00",$yr,$mnth,$day);
    ?>
    Where is the date coming from? User input?
    Is the ddmmyy date actually used in the script logic? Could you just use mysql's date time functions to generate a correctly formatted date in the first place?

  6. #6
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Instead of using printf to show the output, how do I save the output as a variable?

  7. #7
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Zaggs View Post
    Hi,

    Instead of using printf to show the output, how do I save the output as a variable?
    PHP Code:
    $msqldate $year.'-'.$month.'-'.$day

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about like this?
    PHP Code:
    $string "251207";
    $date "20" substr($string42) .
            
    "-" .  substr($string22) .
            
    "-" .  substr($string02);
    $date date('Y-m-d H:i:s'strtotime("$date"));
    echo 
    $date
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Zaggs View Post
    Hi,

    Instead of using printf to show the output, how do I save the output as a variable?
    Use sprintf -
    PHP Code:
    $output sprintf("20%s-%s-%s 00:00:00",$yr,$mnth,$day); 
    Or use Hammer's suggestion, or Rajug's.. there's a million different ways to do this but I'm interested in knowing why are you receiving dates in the ddmmyy format without any delimeters?


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
  •