简体   繁体   English

日期时间格式显示错误的日期

[英]Datetime format showing wrong day

I'm having an odd one which is breaking an invoicing script.我有一个奇怪的问题,它破坏了发票脚本。 I have a payment date of 31st August 2019 and this person is to then be paid on the same day each week which means that this would be every Saturday.我的付款日期为 2019 年 8 月 31 日,然后将在每周的同一天向此人付款,这意味着这将是每个星期六。 However when i enter this as a new DateTime object when formatted with l it says Sunday.但是,当我将其作为新的 DateTime 对象输入时,使用 l 进行格式化时,它会显示为星期日。 PHP is 7.1 PHP 是 7.1

$fpd = new DateTime( date( 'Y-m-d 00:00:00', strtotime( '2019-08-31' ) ) );
var_dump( $fpd ); // returns 2019-08-31 00:00:00.000000 in the date section
echo $fpd->format( 'l' ); returns Sunday

Whats going on?这是怎么回事? is this just me with this?这只是我吗? If i format the date to show the Day i get this (assuming $fpd is as above)如果我格式化日期以显示我得到的日期(假设 $fpd 如上所述)

echo $fpd->format( 'g:ia \o\n l jS F Y' ); returns 12:00am on Sunday 1st September 2019

so confused how can 0h0m on 31st August be 24 hours later?如此困惑,8 月 31 日的 0h0m 怎么会晚 24 小时?

thanks谢谢

Edit: I should point out I'm using WAMP64 and in the uk.编辑:我应该指出我在英国使用 WAMP64。 I tried using php 7.0 but the same result, php 5.6 is not an option as code is php 7 complient.我尝试使用 php 7.0,但结果相同,php 5.6 不是一个选项,因为代码符合 php 7。 I tried just using 'Ymd' but it saves the time in the DateTime object anyway我尝试只使用“Ymd”,但无论如何它都可以节省 DateTime 对象中的时间

$fpd = {DateTime)[3]
date => "2019-08-31 00:00:00.000000",
timezone_type => {int}3,
timezone => "Europe/London"

$fpd->format( 'l' ) still shows Sunday

Further Edit: Corrected (string) in above code to a var_dump.进一步编辑:将上述代码中的(字符串)更正为 var_dump。 running this standalone in its own test.php file works and shows the day as Saturday but it uses the UTC timezone.在它自己的 test.php 文件中运行这个独立文件可以工作并将日期显示为星期六,但它使用 UTC 时区。

This returns Saturday (Php 7.3) tested这将返回星期六(PHP 7.3)测试

<?php
 $fpd = new DateTime( date( 'Y-m-d', strtotime( '2019-08-31' ) ) ); 
 echo $fpd->format( 'l' );
 echo $fpd->format( 'l' ); //returns Sunday
 echo $fpd->format( 'g:ia \o\n l jS F Y' );
 ?>

So it turned out the maths was wrong due to today being after the clocks changed.所以结果证明数学是错误的,因为今天是在时钟改变之后。 I removed any times throughout and the code is now working correctly.我在整个过程中删除了任何时间,代码现在可以正常工作。 Big up to @davidev & @Tschallacka for pointing me in the right direction感谢@davidev 和@Tschallacka 为我指明了正确的方向

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

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