简体   繁体   English

返回的日期不正确

[英]Incorrect day number returned

for some reason I'm getting the correct day name, but incorrect day number when executing the following in PHP... 由于某种原因,我在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. 奇怪的是,在浏览器中调用getDay()时,我用JS返回了4行相同的操作,因此它似乎不是特定于语言的。 Note: I'm using the LAMP stack. 注意:我正在使用LAMP堆栈。

Anyone know why this is? 有人知道为什么吗? Thanks. 谢谢。

The date parameter w you are using is returning the correct value. 您使用的日期参数w返回正确的值。 You need to refer to another parameter to return the result you expect: 您需要引用另一个参数以返回期望的结果:

From the PHP Manual: 从PHP手册:

"w" Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) “ w”星期几的数字表示形式(0表示星期日)至6表示(星期六表示)

You need "d" or "D" to represent the day of the month. 您需要用“ d”或“ D”代表月份中的某天。

<?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 w-从星期日到星期六计数为0到6,这就是为什么您得到4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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