Hi Dev,
Today, we will show you convert datetime into milliseconds in PHP. This article will give you simple example of convert datetime into milliseconds in PHP. Let’s discuss convert datetime into milliseconds in PHP. In this article, we will implement a convert datetime into milliseconds in PHP.
So let’s follow few step to create example of convert datetime into milliseconds in PHP.
Example 1: using DateTime()
index.php
<?php /* Your date string */ $dateString = "2023-01-01 12:34:56"; /* Create a DateTime object */ $dateTime = new DateTime($dateString); /* Get the timestamp in seconds */ $timestampInSeconds = $dateTime->getTimestamp(); /* Convert seconds to milliseconds */ $timestampInMilliseconds = $timestampInSeconds * 1000; /* Output the result */ echo "Date in milliseconds: " . $timestampInMilliseconds; ?>
Output:
Date in milliseconds: 1672576496000
Example 2: using strtotime()
index.php
<?php /* Your date string */ $dateString = "2023-01-01 12:34:56"; /* Convert date string to timestamp in seconds */ $timestampInSeconds = strtotime($dateString); /* Convert seconds to milliseconds */ $timestampInMilliseconds = $timestampInSeconds * 1000; /* Output the result */ echo "Date in milliseconds: " . $timestampInMilliseconds; ?>
Output:
Date in milliseconds: 1672576496000
I hope it will assist you…