2017-07-31 81 views
0

我得到这个代码日期转换mktime返回错误的日期

<?php 
function dateFunc($month) 
{ 
    setlocale(LC_TIME, 'spanish'); 
    $monthSub = date('m',strtotime($month)); 
    $name=strftime("%B",mktime(0,0,0,$monthSub,1,2000)); 
    $name = $name.' '.date('Y', strtotime($month)); 
    return strtoupper($name); 
} 
$output =""; 


$output.= 
dateFunc(date('Y-m', strtotime('-1 month'))).'<br> 
'.dateFunc(date('Y-m', strtotime('-2 month'))).'<br> 
'.dateFunc(date('Y-m', strtotime('-3 month'))).'<br> 
'.dateFunc(date('Y-m', strtotime('-4 month'))).'<br> 
'.dateFunc(date('Y-m', strtotime('-5 month'))).'<br>'; 

echo $output; 
?> 

输出是

JULIO 2017 
MAYO 2017 
MAYO 2017 
MARZO 2017 
MARZO 2017 

I'm期待这个输出

JULIO 2017 
JUNIO 2017 
MAYO 2017 
ABRIL 2017 
MARZO 2017 
有一天我读到这个问题

,冲突发生在30天的月份,mktime()需要另一个参数,但我不记得如何解决它。

建议请...

回答

0

这里是我怎么可能去做。
https://iconoun.com/demo/temp_alberto.php

<?php // demo/temp_alberto.php 
 
/** 
 
* Month computations 
 
* 
 
* https://stackoverflow.com/questions/45422134/date-conversion-mktime-returns-wrong-date 
 
*/ 
 
error_reporting(E_ALL); 
 
echo '<pre>'; 
 

 

 
function lastMonth($date='next month') 
 
{ 
 
    $m = date('Y-m-1', strtotime($date)); 
 
    $x = date('F Y', strtotime($m . '- 1 day')); 
 
    return strtoupper($x); 
 
} 
 

 
$out = NULL; 
 

 
$lm = lastmonth(); 
 
$out .= PHP_EOL . $lm; 
 

 
$lm = lastmonth($lm); 
 
$out .= PHP_EOL . $lm; 
 

 
$lm = lastmonth($lm); 
 
$out .= PHP_EOL . $lm; 
 

 
$lm = lastmonth($lm); 
 
$out .= PHP_EOL . $lm; 
 

 
$lm = lastmonth($lm); 
 
$out .= PHP_EOL . $lm; 
 

 
echo $out;

0

你可以这样说:

$output.= 
dateFunc((new DateTime(date("Y-m-1")))->modify("-5 month")->format("Y-m")); 
echo $output; 
+0

@阿尔贝托 - siurob请接受帮你答案。 – Vladislav