SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast drownedbeaver's Avatar
    Join Date
    Aug 2011
    Location
    Philippines
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Format date time

    Hey guys,
    Have you tried to format something like this ---> Fri, 07 Oct 2011 23:28:23
    THEN TURN INTO THIS -------> 2011-10-07 23:28:23?

    Thanks for the help guys......

  2. #2
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    First of all, you'll want to use the following string to format your final date:

    Code PHP:
    $dateFormat = 'Y-m-d H:i:s';

    You could just pass that to the date function ("date($dateFormat)"), but that would just format the current date/time. To format an arbitrary date, you also have to pass it a Unix timestamp, like so:

    Code PHP:
    $final = date($dateFormat, $unix);

    And you can get a Unix timestamp from a well-formatted string with the strtotime function:

    Code PHP:
    $starting = 'Fri, 07 Oct 2011 23:28:23';
    $unix = strtotime($starting);

    So when it's all said and done, your code would look something like this:

    Code PHP:
    $starting = 'Fri, 07 Oct 2011 23:28:23';
    $unix = strtotime($starting);
    $dateFormat = 'Y-m-d H:i:s';
    $final = date($dateFormat, $unix); // should return '2011-10-07 23:28:23'
    I'm the web overlord for Graphic Business Systems

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $old"Fri, 07 Oct 2011 23:28:23";
    $tgt "2011-10-07 23:28:23";

    $new date('Y-m-d H:i:s',strtotime($old));
    if( 
    $new === $tgt ) echo 'Bingo!'
    If $old is coming from a dubious source also look at checkdate.

    All the date format options are listed in the manual date.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    If you're lucky enough to be allowed to use PHP 5.3 (and you should be, since the PHP Group no longer support 5.2), then you can easily take your input date and format it exactly how you like.

    PHP Code:
    $subject   "Fri, 07 Oct 2011 23:28:23";
    $datetime  DateTime::createFromFormat("D, d M Y H:i:s"$subject);
    $formatted $datetime->format("Y-m-d H:i:s"); 
    The above is the preferred option when you know the format of the date string coming in. If you're not using PHP 5.3, or the input date string varies in format, then new DateTime(…) or strtotime(…) can be used so long as it consists of supported date/time formats.

    More info:
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Enthusiast drownedbeaver's Avatar
    Join Date
    Aug 2011
    Location
    Philippines
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys
    Thanks for the interesting replies, hmmmm..... It says on my xampp panel the php is 5.3.* how come Salathe's code didn't work.
    I just followed the code and he give an error Fatal error: Call to a member function format() on a non-object

    Thanks CUPS it worked, Thanks also sdleihssirhc for the reply.

    This is SOLVED!


Tags for this Thread

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
  •