SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Formatting a date string

    Hi,

    I have a date stored in a string. I want to format this date into format like "Tuesday, October 18, 2011 1:22 PM". How can I do this? I tried some of the functions like strftime / date but no luck.

  2. #2
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You want strtotime() and date()

    Eg
    PHP Code:
    $time_str '2nd November 2011';
    $time strtotime($time_str);

    echo 
    date('l, F j, Y, g:i A'$time);
    //Wednesday, November 2, 2011, 12:00 AM 

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot travo! It provides the expected format, along with one warning message as below.

    Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Kolkata' for '5.5/no DST' instead in
    <php file name> on line 43

  4. #4
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This just means you need to set your default timezone in one of the two ways mentioned... I usually like to do it in the php.ini file as the error shouldnt appear after you have done so.

    See http://php.net/manual/en/function.da...mezone-set.php and http://www.php.net/manual/en/timezones.php


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
  •