php, date() 未返回正确的日期/时间

发布于 2025-01-04 21:00:36 字数 391 浏览 0 评论 0原文

下面的代码...

$date = "02-13-2012";
$start_time = "17:30";
$end_time = "20:00";

$start_timestamp = date("m-d-Y H:i",strtotime($date." ".$start_time));
$end_timestamp = date("m-d-Y H:i",strtotime($date." ".$end_time));

print($start_timestamp);
print($end_timestamp);

返回...

1969-12-31 19:30:00

1969-12-31 20:30:00

有谁知道为什么这不能正常工作?

The code below...

$date = "02-13-2012";
$start_time = "17:30";
$end_time = "20:00";

$start_timestamp = date("m-d-Y H:i",strtotime($date." ".$start_time));
$end_timestamp = date("m-d-Y H:i",strtotime($date." ".$end_time));

print($start_timestamp);
print($end_timestamp);

Returns...

1969-12-31 19:30:00

1969-12-31 20:30:00

Does anyone have any idea why this is not working correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慢慢从新开始 2025-01-11 21:00:36

02-13-2012 17:30 不是可识别的日期格式。使用日-月-年或年-月-日顺序,或者使用自定义解析日期格式,例如 DateTime::createFromFormat

02-13-2012 17:30 is not a recognized date format. Either use day-month-year or year-month-day order, or custom parse the date format using, for example, DateTime::createFromFormat.

吃→可爱长大的 2025-01-11 21:00:36

看到这个。我认为你必须更改日期格式。

$date = "02-13-2012";
$date = str_replace("-","/",$date);
$start_time = "17:30";
$end_time = "20:00";

$start_timestamp = date("m-d-Y H:i",strtotime($date." ".$start_time));
$end_timestamp = date("m-d-Y H:i",strtotime($date." ".$end_time));

print($start_timestamp);
echo "<br/>";
print($end_timestamp);

See this. I think you have to change the date format.

$date = "02-13-2012";
$date = str_replace("-","/",$date);
$start_time = "17:30";
$end_time = "20:00";

$start_timestamp = date("m-d-Y H:i",strtotime($date." ".$start_time));
$end_timestamp = date("m-d-Y H:i",strtotime($date." ".$end_time));

print($start_timestamp);
echo "<br/>";
print($end_timestamp);
高跟鞋的旋律 2025-01-11 21:00:36

您的日期格式对于 strtotime 函数不正确,

要么更改为

 $date = "13-02-2012";

或更改为其他有效格式

your date format is not correct for strtotime function

either change is to

 $date = "13-02-2012";

or to some other valid format

刘备忘录 2025-01-11 21:00:36

当您将日期格式化为 xx-yy-zzz 时,strtotime() 会将其解释为正常格式的日期,而不是美国格式的日期,因此它是 dd -mm-yyyy

When you format your date like xx-yy-zzz, strtotime() interprets it as a date in the normal format, not the American one, so it's dd-mm-yyyy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文