SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    date('Y') or date('o')?

    Hello,

    I want to make sure that when January 1st comes, and only January 1st, the function will return the accurate year.

    I know that 'o' is ISO related, and I'm not sure if that's the one I should use or not.


  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Given that January 1st next year is a Sunday, it'll still be part of the previous week so 'o' will return 2011. Try it:

    PHP Code:
    echo date('o'strtotime('1st January 2012')). '<br>';
    echo 
    date('Y'strtotime('1st January 2012')); 
    I'm not really sure why you'd want to use anything other than 'Y'?
    Martin.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to make sure that 'Y' doesn't do what you just described for 'o'.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rhgiant View Post
    I need to make sure that 'Y' doesn't do what you just described for 'o'.
    Rest assured, it doesn't.
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rhgiant View Post
    I need to make sure that 'Y' doesn't do what you just described for 'o'.
    Always see documentation if you get confused:
    http://www.php.net/manual/en/function.date.php
    What is the difference in Y and o or which one is suitable for you!
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    'o' is intended to be used with 'W' as that particular version of the year is specifically related to which week of the year that it is. Between the 1st and 3rd January the year in 'o' can be one less than in 'Y' and between 29th and 31st December it can be one more as the 'o' year is always that of the closest Thursday so that week one according to 'W' always has at least 4 days.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •