在PHP中需要DateTime的相对格式

时间:2012-08-30 20:24:28

标签: php datetime timestamp

我试图在这样的时间戳中找到该月的第n天:

$day = 15;

$date = new DateTime('@' . $timestamp);
$date->modify($day . ' day of current month');

这会产生错误:

Warning: DateTime::modify(): Failed to parse time string (15 day of current month) at position 7

我也尝试过“当月的第15天”,但这不起作用。

如何修改我的“修改”以在时间戳中找到当月的第n天?

2 个答案:

答案 0 :(得分:1)

首先检索当月的第一天,然后添加所需的天数。

$date = new DateTime('first day of ' . date("Y-m-d", $timestamp));
$date->modify('+' . ($day-1) . 'days');

答案 1 :(得分:0)

你可以使用php的date()格式化功能和一个硬设置的“day”值(根据需要进行调整):

<?php
$timestamp = time();
$format = date('Y-15-M', $timestamp);
$fifteenth = strtotime($format);
echo "15th was a " . date("D", $fifteenth) . "\n";