During the development of the project, we had to change the format for displaying the dates of entries. The article creation module saves the date in the MySQL DATETIME format, that is, of the form 2016-12-28 14:22:10 You can easily change the format for saving the date, but then other php handlers that take date information from the database whose records have accumulated decently I need a record of the form 28 дек'16 in a certain block and under hover 14:22:10 from 2016-12-28 14:22:10

Question: Are there any standard php functions that from a string like 2016-12-28 14:22:10 will give me a string of type 28 дек'16 ?

    3 answers 3

    Yes, see the DateTime class. And in particular, the createFromFormat function, there are also possible formats for output and for parsing the string

    In general, it will work like this:

     $format = 'Ymd H:i:s'; $date = DateTime::createFromFormat($format, '2009-02-15 15:16:17'); echo "в определенном блоке " . $date->format('dm Y') . "\n"; echo "и под hover " . $date->format('H:i:s') . "\n"; 

      there is. You can set the date / time format.

       //28 дек 16` echo $date('d M y'); 

      You can read about the format - http://php.net/manual/ru/function.date.php

      • No, I do not need to display the current date and time, but the ones written in the database as a string. - Daddyman
      • I described the essence. the rule in brackets is also valid for DateTime :: createFromFormat, which was advised to you above - nueq

      Perhaps my functions, which I wrote for my engine, will help.

        function getMonthName($no) { $month = ''; if($no == '01') $month = 'января'; elseif($no == '02') $month = 'февраля'; elseif($no == '03') $month = 'марта'; elseif($no == '04') $month = 'апреля'; elseif($no == '05') $month = 'мая'; elseif($no == '06') $month = 'июня'; elseif($no == '07') $month = 'июля'; elseif($no == '08') $month = 'августа'; elseif($no == '09') $month = 'сентября'; elseif($no == '10') $month = 'октября'; elseif($no == '11') $month = 'ноября'; elseif($no == '12') $month = 'декабря'; return $month; } function getWordDateTime($date, $no_year = false) { if($date != '') { $dtArr = explode(' ', $date); if(isset($dtArr[0])) { $arr = explode('-', $dtArr[0]); $ret = $arr[2] . ' ' . getMonthName($arr[1]); if (!$no_year) $ret .= ' ' . $arr[0]; } if(isset($dtArr[1])) $ret .= $dtArr[1]; return $ret; } else return ''; }