Home > Article > Backend Development > How to convert php time format
PHP time format conversion method: 1. Convert the date to a timestamp through the "strtotime(time,now);" function; 2. Format the timestamp through the "date(format, timestamp)" function for the date and time.
#Environment of this article: Windows7 system, PHP7.1, Dell G3 computer.
Recommended: "PHP Video Tutorial"
php time format conversion
The conversion function of php time format is date( ), strtotime() function, PHP’s native time class can also convert time format.
1. Convert Y-m-d to timestamp. Example: 2017-08-22 is converted to timestamp strtotime('2017-08-22');
2. Convert timestamp to Y-m-d H: i:s date("Y-m-d H:i:s",strtotime('2017-08-22'));
3. The time Ymd format is converted into Y-m-d date("Y-m-d",strtotime("20170822 "));
You can also directly convert using native PHP classes var_dum(\DateTime::createFromFormat('Ymd','20170822')->format('Y-m-d'));
4. Get the current timestamp: 1. time(); 2. date('U');
5. Tomorrow's time format date("Y-m-d H:i:s",strtotime( 1 day));
Get the date within a period of time The searched code
$end = new \DateTime($end); $end = $end->modify( '+1 day' ); $interval = new \DateInterval('P1D');// yii中引用原生的php类加\,因为有命名空间 $daterange = new \DatePeriod($start, $interval ,$end);//查询这个时间段内所有的日期 foreach($daterange as $date){ $single_date = $date->format("Ymd");//每个日期都改成20170022的格式 $this->run_curl($url,$post_data,$project,$flow,$single_date,$timeBegin,$timeEnd); }
$datarange is the date within the time period.
The above is the detailed content of How to convert php time format. For more information, please follow other related articles on the PHP Chinese website!