Can a timestamp format be done?

2
 // $timestamp    = "";
  $datetimeFormat = 'YmdHis';
  $date = new DateTime('now');
 // $date->setTimestamp($timestamp);      
  echo $date->format($datetimeFormat);

The result of that code is: 20180411131121

    
asked by Guillermo Alejandro Prez Hervs 11.04.2018 в 13:12
source

2 answers

5

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

    
answered by 11.04.2018 / 13:28
source
0

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.

    
answered by 11.04.2018 в 15:53