1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Convert date format with PHP

Discussion in 'PHP' started by Kyriakos, Apr 18, 2013.

  1. #1
    Hi,
    I want to convert this date format 18/4/2013 to this format 2013-04-18

    How i can do this with PHP?
    thanks in advance.
     
    Kyriakos, Apr 18, 2013 IP
  2. sparky21289

    sparky21289 Member

    Messages:
    97
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    45
    #2
    
    <?php
    $date = new DateTime('2000-01-01');
    echo $date->format('Y-m-d');
    ?>
    
    Code (markup):
     
    sparky21289, Apr 18, 2013 IP
    jags2ooo likes this.
  3. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Where i can put the string of the initial date format?
     
    Kyriakos, Apr 18, 2013 IP
  4. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    i want to convert the date format from d/m/yyyy to yyyy-mm-dd
     
    Kyriakos, Apr 18, 2013 IP
  5. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #5
    Hm, DateTime is a good solution, though I'm not too sure if d/m/Y is a valid format you can pass through to the class constructor. I suspect it may raise an exception. Likewise, you may not be able to convert that string to a Unix timestamp using strtotime in that format as well (though it's worth trying). If that's the case, a simple solution may be to do something like:

    <?php
     
    list($day, $month, $year) = explode('/', '18/4/2013');
    $convertedDate = sprintf('%s-%s-%s', $year, $month, $day);
    var_dump($convertedDate);
    PHP:
    Output:
    string(9) "2013-4-18"
    Code (markup):
     
    Alex Roxon, Apr 18, 2013 IP
  6. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    this is my solution
    $md = explode("/", "18/4/2013"); // split the array
    $nd = $md[2]."-".$md[1]."-".$md[0]; // join them together
    $mydate = date('Y-m-d', strtotime($nd));
    PHP:
     
    Kyriakos, Apr 18, 2013 IP
  7. q7m

    q7m Well-Known Member

    Messages:
    1,178
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    1
    #7
    I suggest using strtotime function to convert the date to a unix timestamp, then you can display it on any format you wish, eg:
    $format1 = "18/4/2013";
    $timestamp = strtotime($format1);
    $format2 = date("Y-m-d", $timestamp);
    Code (markup):
     
    q7m, Apr 18, 2013 IP
  8. Alex Raven

    Alex Raven Greenhorn

    Messages:
    5
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    23
    #8
    Relying on php date functions such as strtotime is not safe, because they are using server's locale which also describes date and time formats. You need to specify both formats:

    <?
    $dt = DateTime::createFromFormat("d/m/Y", "18/4/2013");
    echo $dt->format("Y-m-d");
    ?>
    Code (markup):
     
    Alex Raven, Apr 18, 2013 IP
    Alex Roxon likes this.
  9. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #9
    This will not work, since your format is: d/m/Y. As stated on the manual:

    Which is the solution Alex Raven posted.
     
    ThePHPMaster, Apr 18, 2013 IP
  10. designpack

    designpack Banned

    Messages:
    7
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    8
    #10
    You can use explode to break string into array using $arr=explode("-",$da); and then reverse array using array_reverse($arr); and then implode an array using $str= implode("-",$arr); so you will get your result in the $str
     
    designpack, Apr 18, 2013 IP
    worldart and technoguy like this.