// $timestamp = "";
$datetimeFormat = 'YmdHis';
$date = new DateTime('now');
// $date->setTimestamp($timestamp);
echo $date->format($datetimeFormat);
The result of that code is: 20180411131121
// $timestamp = "";
$datetimeFormat = 'YmdHis';
$date = new DateTime('now');
// $date->setTimestamp($timestamp);
echo $date->format($datetimeFormat);
The result of that code is: 20180411131121
Try this way:
<?php
// $timestamp = "";
$datetimeFormat = 'Y-m-d H:i:s';
$date = new DateTime('now');
// $date->setTimestamp($timestamp);
echo $date->format($datetimeFormat);
?>
This is the result:
2018-04-11 11:28:53
Here you have a complete explanation of how to format a timestamp
If what you need is the timestamp
you can use the getTimestamp()
method of the object DateTime
.
$date = new DateTime('now');
var_dump($date->getTimestamp());
Exit:
int(1523454056)
But if what you need is to format it, you were right, only that you needed to give it what it needed to be visually correct.
$datetimeFormat = 'Y-m-d H:i:s';
$date = new DateTime('now');
var_dump($date->format($datetimeFormat));
Exit:
string(19) "2018-04-11 13:44:02"
Here you have the accepted formats for DateTime()
.
And this is the code running so you can see the different results.