Results 1 to 6 of 6

Thread: how to change date format ?

  1. #1
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default how to change date format ?

    i have date stored as type date: y-m-d (eg 2003-05-03)

    i want it to be display as

    3 May 2003

    i tried this
    PHP Code:
    $dateStartdate("j M y"strtotime($row['dateStart'])); 
    but the result displayed 1 Jan 70

    help
    Last edited by sabahan; 09-16-2009 at 12:58 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It's best to store dates as the number of seconds since 1970, which is the standard format. It's something like 11000000 now, though I forget the number of zeros.
    That is what date() is setup to deal with, so you can use that to output how you want and strtotime to make that.
    I'm not sure about why it's not working, but there are probably ways to set strtotime to understand y-m-d.
    But again, it's best to just always start with the default date() input (as just number of seconds since 1970) and it'll always work.

    Hope this helps.

    (Specifically about your code-- the output is correct, but the input is not. strtotime is not giving a reasonable value, so date() is assuming 0 seconds since 1970-- so you get 1 Jan 1970.)
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    sabahan (09-16-2009)

  4. #3
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Your code looks like it should work, maybe check how you've stored it in the database, this definitely works:

    PHP Code:
    $datetime '2003-05-03';

    echo 
    date('j M y'strtotime($datetime));

    // Outputs 3 May 03 

  5. The Following User Says Thank You to Schmoopy For This Useful Post:

    sabahan (09-16-2009)

  6. #4
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    thanks..the date in db is correct and is stored as 2003-05-03

    i use it like this and now it works

    PHP Code:

    echo "" date('j M Y'strtotime($row['dateStart'])) . ""

    but i wonder why when i used it like this..the date is displayed as 1 Jan 1970

    PHP Code:
    $mydatedate('j M Y'strtotime($row['dateStart']));

    echo 
    $mydate

  7. #5
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    need another help......

    i have dateStart and dateEnd

    both stored as xxxx-xx-xx format in db eg 2009-09-16

    i displayed the date in my page as 16 Sep 09


    PHP Code:
     echo "" date('j M y'strtotime($row['dateStart'])) . "";

    echo 
    " - ";

    echo 
    "" date('j M y'strtotime($row['dateEnd'])) . ""
    example the date duration is 16 Sep 09 - 18 Sep 09


    i want to display it as 16 - 18 Sep 09


    and if the date is 29 Jan 09 - 3 Feb 09

    i want to display it as 29 Jan - 3 Feb 09

    is this possible ?

  8. #6
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    ok solved

    PHP Code:

    $month1 
    date('M'strtotime($row['dateStart']));
    $month2 date('M'strtotime($row['dateEnd']));

     if (
    $row[dateEnd]=='0000-00-00')
    {
       echo 
    date('j M y'strtotime($row['dateStart']));  
    }
     
     
    else if (
    $month1 == $month2) {

      echo 
    date('j'strtotime($row['dateStart'])) . ' - ' date('j M y'strtotime($row['dateEnd']));  

    } else {

      echo 
    date('j M y'strtotime($row['dateStart']) . ' - ' date('j M y'strtotime($row['dateEnd']);



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
  •