Home > Article > Backend Development > How to convert php time format
php time format conversion
The php time format conversion functions include date(), strtotime() functions, PHP's native time class can also convert time formats.
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 for a period of time
$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.
Recommended: "PHP Tutorial"
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!