Home > Mobile >  How to make a datetime a bigint?
How to make a datetime a bigint?

Time:11-08

I want to convert a datetime to big int. For Example. I have an entry in the file that i read in php like normal datetime, "2021/05/20 11:46"(i keep it in a variable named $data) that i want to transform into a bigint like 202105201146 (year month day hour minute) . I tried to work around and make it working, but I still get the error :

"Argument #2 ($timestamp) must be of type ?int, string given in". >

The line in question is :

$ID= date('Y', $data) . date('m', $data) . date('d', $data). date('G', $data) . date('i', $data). date('s', $data);

CodePudding user response:

You must first convert it into a datetime object, and then format it as "bigint":

$dateAsString = '2021/05/20 11:46';
$datetime = \DateTime::createFromFormat('Y/m/d h:i', $dateAsString);
echo $datetime->format('Ymdhi'); // 202105201146

However, since the error is asking for a timestamp (as int value), it could be better to use the getTimestamp() method of DateTime class, which returns the epoch timestamp:

$dateAsString = '2021/05/20 11:46';
$datetime = \DateTime::createFromFormat('Y/m/d h:i', $dateAsString);
$timestamp =  $datetime->getTimestamp(); // 1621511160

CodePudding user response:

Assuming that the month, day, hour and minutes are always zero padded, just replace all non-digits from your "string":

echo preg_replace("/\\D /", "", "2021/05/20 11:46");
  • Related