Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从时间戳中获取上个月的名称_Php_Date_Unix Timestamp - Fatal编程技术网

PHP从时间戳中获取上个月的名称

PHP从时间戳中获取上个月的名称,php,date,unix-timestamp,Php,Date,Unix Timestamp,我有一个Unix时间戳,想知道前一个月的名字e。G“狂热的” 你的朋友在吗 echo Date('F', strtotime($date . " last month")); 对于希望此完全动态、始终显示上月姓名的任何人,代码如下: $currentMonth = date('F'); echo Date('F', strtotime($currentMonth . " last month")); 您可以将DateTime对象设置为指定的时间戳,然后减去间隔'P1M'(一个月),如下所示:

我有一个Unix时间戳,想知道前一个月的名字e。G“狂热的”

你的朋友在吗

echo Date('F', strtotime($date . " last month"));
对于希望此完全动态、始终显示上月姓名的任何人,代码如下:

$currentMonth = date('F');
echo Date('F', strtotime($currentMonth . " last month"));

您可以将
DateTime
对象设置为指定的时间戳,然后减去间隔
'P1M'
(一个月),如下所示:

/**
 * @param {int} $date unix timestamp
 * @return string name of month
 */
function getLastMonth($date) {
    // create new DateTime object and set its value
    $datetime = new DateTime();
    $datetime->setTimestamp($date);
    // subtract P1M - one month
    $datetime->sub(new DateInterval('P1M'));

    // return date formatted to month name
    return $datetime->format('F');
}

// Example of use
$date = 1489842000;
$lastMonth = getLastMonth($date);

如果你在那里解释你的代码,这将是一个更好的答案!虽然这个代码片段是受欢迎的,并可能提供一些帮助,但它将是如何以及为什么解决这个问题。记住,你是在将来回答读者的问题,而不仅仅是现在提问的人!请在回答中添加解释,并说明适用的限制和假设。@TobySpeight解释补充道
/**
 * @param {int} $date unix timestamp
 * @return string name of month
 */
function getLastMonth($date) {
    // create new DateTime object and set its value
    $datetime = new DateTime();
    $datetime->setTimestamp($date);
    // subtract P1M - one month
    $datetime->sub(new DateInterval('P1M'));

    // return date formatted to month name
    return $datetime->format('F');
}

// Example of use
$date = 1489842000;
$lastMonth = getLastMonth($date);