PHP 函数仅返回 DateTime 格式的日期

发布于 2025-01-14 10:01:02 字数 209 浏览 0 评论 0原文

我有一个日期时间格式 '2022-03-17T15:00:00+02:00' 的字符串,我需要一个 php 函数来仅返回日期 '2022-03-17 '。我没有找到解决办法。字符串 '2022-03-17T15:00:00+02:00' 在 xml feed 中是可变的。它每天都在变化,因此必须对其进行格式化以包含在函数中。

I have a string with the DateTime format '2022-03-17T15:00:00+02:00' and I need a php function to return only the Date '2022-03-17'. I didn't find a solution. The string '2022-03-17T15:00:00+02:00' is variable in the xml feed. It change every day, so it must formatted to included in the function.

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

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

发布评论

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

评论(4

天邊彩虹 2025-01-21 10:01:02

strtotime 不适合与 date 一起转换包含时区的日期,例如此处的 +02:00。与其他时区相关时可能会出现错误。示例:

date_default_timezone_set('America/New_York');

$time = '2022-03-17T01:00:00+02:00';
echo date("Y-m-d", strtotime($time)); 
//2022-03-16

亲自看看

这里通常建议使用 DateTime,因为时区处理正确。

date_default_timezone_set('America/New_York');

$time = '2022-03-17T01:00:00+02:00';
echo date_create($time)->format('Y-m-d'); 
//2022-03-17

strtotime is not suitable in conjunction with date to convert a date that contains a time zone such as +02:00 here. Errors may occur in connection with other time zones. Example:

date_default_timezone_set('America/New_York');

$time = '2022-03-17T01:00:00+02:00';
echo date("Y-m-d", strtotime($time)); 
//2022-03-16

See for yourself!

The use of DateTime is generally recommended here, since the time zones are handled correctly.

date_default_timezone_set('America/New_York');

$time = '2022-03-17T01:00:00+02:00';
echo date_create($time)->format('Y-m-d'); 
//2022-03-17
微凉徒眸意 2025-01-21 10:01:02
store your date time in a variable and try this:

$date_to_be_formatted = '2022-03-17T15:00:00+02:00';
echo get_date($date_to_be_formatted);

function get_date($date_to_be_formatted){
    $formatted_date = date('Y-m-d',strtotime($date_to_be_formatted));
    return $formatted_date;
  }
    
    output:
    2022-03-17
store your date time in a variable and try this:

$date_to_be_formatted = '2022-03-17T15:00:00+02:00';
echo get_date($date_to_be_formatted);

function get_date($date_to_be_formatted){
    $formatted_date = date('Y-m-d',strtotime($date_to_be_formatted));
    return $formatted_date;
  }
    
    output:
    2022-03-17
晨光如昨 2025-01-21 10:01:02
echo date("Y-m-d", strtotime($time));

https://www.php.net/manual/en/function.date。 php

echo date("Y-m-d", strtotime($time));

https://www.php.net/manual/en/function.date.php

甜味超标? 2025-01-21 10:01:02

@Denis 和 @Relcode

该功能现在正常工作:

function get_date( $time ) {
$time = '2022-03-17T15:00:00+02:00';
echo date("Y-m-d", strtotime($time)); 
}

echo get_date('2022-03-17T15:00:00+02:00');

输出:2022-03-17

@Denis and @Relcode

That function now working properly:

function get_date( $time ) {
$time = '2022-03-17T15:00:00+02:00';
echo date("Y-m-d", strtotime($time)); 
}

echo get_date('2022-03-17T15:00:00+02:00');

Output: 2022-03-17

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