Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Convert one date format into another in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 221
    Comment on it

    Date play an important role in any project. We have to change date format according to our need. If we are using PHP 5.2 and lower then for changing date format we have to parse the elements (year, month, day, hour, minute, second) manually using substr() and hand the results to mktime() that will build you a timestamp or we will use strtotime and then change the format like this.

    $result=date('Y-m-d H:i:s',strtotime($dateValue)); 
    

    But for PHP 5.3 and higher version we will use DateTime::createFromFormat. It allows you to specify an exact mask - using the date() syntax - to parse incoming string dates with.

    Example:

    <?php
    $date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
    echo $date->format('Y-m-d');
    ?>
    

    This is the Object oriented style to convert one date format into another in PHP.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: