从php中的日期时间获取年/月/日?

时间:2022-09-27 20:47:13

I used date('w', timestamp) and date('w', timestamp) to know the day, date('n', timestamp) for months, etc.

我使用日期('w',时间戳)和日期('w',时间戳)来了解几个月的日期,日期('n',时间戳)等。

Now I'm using datetime and I'd like to know what are the equivalent functions to get a day, a month, etc from a datetime.

现在我正在使用datetime,我想知道从日期时间获得一天,一个月等的等效函数。

PS: I know I could use UNIX_TIMESTAMP() in a SQL query but I prefer avoiding timestamps using in my code.

PS:我知道我可以在SQL查询中使用UNIX_TIMESTAMP(),但我更喜欢在代码中避免使用时间戳。

2 个解决方案

#1


77  

Use DateTime with DateTime::format()

将DateTime与DateTime :: format()一起使用

$datetime = new DateTime($dateTimeString);
echo $datetime->format('w');

#2


21  

Check out the manual: http://www.php.net/manual/en/datetime.format.php

查看手册:http://www.php.net/manual/en/datetime.format.php

<?php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
?>

Will output: 2000-01-01 00:00:00

将输出:2000-01-01 00:00:00

#1


77  

Use DateTime with DateTime::format()

将DateTime与DateTime :: format()一起使用

$datetime = new DateTime($dateTimeString);
echo $datetime->format('w');

#2


21  

Check out the manual: http://www.php.net/manual/en/datetime.format.php

查看手册:http://www.php.net/manual/en/datetime.format.php

<?php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
?>

Will output: 2000-01-01 00:00:00

将输出:2000-01-01 00:00:00