简体   繁体   中英

Incorrect day number returned

for some reason I'm getting the correct day name, but incorrect day number when executing the following in PHP...

  date_default_timezone_set('Europe/Helsinki');
  echo "date('l'): ".date('l'); // returns Thursday
  echo "date('w'): ".date('w'); // returns 4

  $dt = new DateTime();
  var_dump($dt); // matches local time and date

  object(DateTime)[24]
    public 'date' => string '2016-09-08 14:44:37' (length=19)
    public 'timezone_type' => int 3
    public 'timezone' => string 'Europe/Helsinki' (length=15)

  echo $dt->format('w'); // returns 4

Strangely enough, I get 4 returned doing the same thing with JS in the browser calling getDay() so it doesn't appear to be language specific. Note: I'm using the LAMP stack.

Anyone know why this is? Thanks.

The date parameter w you are using is returning the correct value. You need to refer to another parameter to return the result you expect:

From the PHP Manual:

"w" Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)

You need "d" or "D" to represent the day of the month.

<?php date('d', $timestamp); // returns day of month with leading 0's ?>

Reason is this

 w gives Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) 

w - gives 0 to 6 counting from sunday to saturday that's why you getting 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM