php 解决月份日期问题

bzzcjhmw  于 2023-03-11  发布在  PHP
关注(0)|答案(3)|浏览(95)
$myDate = date('m-d-Y 00:00:00', strtotime($myDate));
if($debugs)echo"<br>myDate is: $myDate in line: ".__line__."<br>";
$mysqlstart = date('Y-m-d 00:00:00', strtotime($myDate));
if($debugs)echo"<br>mysqlstart is: $mysqlstart in line: ".__line__."<br>";

根据上面的代码,我希望在将$mysqlstart传递到查询中时向观众显示$myDate。
当我回应我的查询时,月份在日期点,日期在月份点,所以得到的不是$mysqlstart为'2023-03- 10',而是输出'2023-10- 03',非常奇怪。
我尝试了上面的代码,期望日期和月份为SQL目的切换,但它以不正确的模式交换了月份和日期。

zbdgwd5y

zbdgwd5y1#

$myDate的前一个值是什么?在代码的第一行中,你用它来把它的值传递给strtetime。尝试类似这样的方法来获取日期,显然你可以把你想要的日期传递给mktime:

<?php $myDate = mktime(0,0,0,03,25,2023);
echo $mysqlstart = date('Y-m-d H:i:s', $myDate); ?>
o0lyfsai

o0lyfsai2#

您可以使用DateTime类来获得结果。
这里是一个小例子

<?php
// Creating a new Datetime Object
$myDate = new DateTime();

//Setting the time at 00:00:00
$myDate->setTime(00,00,00);

//Formatting the date in a readable format
$myDateReadable = $myDate->format('m-d-Y H:i:s');

//Print out the readable version of the date for debug
echo $myDateReadable;

//adding a new line to get a clearer output.
//if you need to use this in the browser, you need to use the br tag instead of \n or the nl2br function.
echo "\n";
//Formatting the date in a sql convenient way format
$mysqlstart = $myDate->format('Y-m-d H:i:s');

//Print out the generate date
echo $mysqlstart;

脚本的输出如下所示:

03-10-2023 00:00:00
2023-03-10 00:00:00
3qpi33ja

3qpi33ja3#

这只是一个猜测,但你的电脑在安装在英语或英国或美国我认为这是你的问题,祝你好运,让我知道,如果你得到它修复,谢谢伊恩。

相关问题