Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert number to date in php

I need to convert a variable

$date = 20130521201511

to

$date = 2013-05-21 20:15:11

however I am not quite sure how to do so.

like image 784
user2394289 Avatar asked May 20 '26 16:05

user2394289


2 Answers

This is achievable using the DateTime class.

$date = DateTime::createFromFormat("YmdHis","20130521201511");
var_dump($date);

Look closely at the documentation of the createFromFormat method to be sure to use the correct format parameters.

like image 190
Bgi Avatar answered May 22 '26 05:05

Bgi


Use DateTime

php $oldDate= 20130521201511;
$date = DateTime::createFromFormat('YmdGis', $oldDate);
  echo $date->format('Y-m-d G:i:s');
like image 30
xsearingheaven Avatar answered May 22 '26 06:05

xsearingheaven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!