Php - mktime or strtotime?

I am trying to convert 2010-02 to February 2010. But I keep getting December 1969

I tried using mktime, strtotime and some combination of the two, but still could not do it ...

This is what I tried recently ...

$path_title = date('F, Y', mktime(0,0,0,2,0,2010)); 
+4
source share
2 answers

This will be the way to do this:

 $dateString = '2010-02'; list($year, $month) = explode('-', $dateString); $timeStamp = mktime(0, 0, 0, $month, 1, $year); echo date('F, Y', $timestamp); 

Another way:

 $dateString = '2010-02'; $timestamp = strtotime($dateString . '-01'); echo date('F, Y', $timestamp); 

strtotime cannot handle ambiguous dates such as "2010-02", but if you make a full date, it should work.

Otherwise, you may need to look at something like DateTime::createFromFormat .

+8
source

Try the following:

 $str = '2010-02'; echo date('F, Y',mktime(0,0,0,substr($str,-2),1,substr($str,0,4))); 

You must ensure that you are using valid mktime() values. In your example, which you edited in the question, you have 0 as the day, which is actually the first day minus one that puts you in the previous month.

0
source

Source: https://habr.com/ru/post/1300694/


All Articles