How do I get a unix timestamp from PHP date time?

Asked
Active3 hr before
Viewed126 times

8 Answers

timestamp
90%

Returns the Unix timestamp representing the date. ,This function has no parameters., Gets the Unix timestamp. ,DateTime::setTimestamp() - Sets the date and time based on an Unix timestamp

1272509157
88%

Is there anyone sitting on a PHP function to convert a date of format 0000-00-00 00:00:00(datetimesql) to unix timestamp?, Alnitak has a valid point, there are a lot of assumptions concerning whether a DB is involved or not wvanbergen. What if he's grabbing timestamps from filenames? – dcousineau Jan 18 '09 at 20:00 ,@bartek - As you noted, PHP's strtotime function is perfect for this. It can handle most common date formats, including strings like "tomorrow" or "+5 months"., yup, in fact the manual doesn't actually say whether this particular date-time format is supported. That's why I recommended strptime() instead. – Alnitak Jan 19 '09 at 0:46

To do this in PHP 5.2 you can do this:

$datetime = new DateTime();
echo $datetime - > format('U');

A newer way to do this as of PHP 5.3 is:

$datetime = new DateTime();
echo $datetime - > getTimestamp();
load more v
72%

To do this in PHP, we can use the strtotime function like so:,By the way, in order to get the current timestamp, you can simply call PHP’s time function like so:,Note that the strtotime function will return a boolean FALSE value if the conversion fails.,For the sake of this example, let’s say that we want to convert 2019-04-01 10:32:00 into an Epoch timestamp.

To do this in PHP, we can use the strtotime function like so:

//Our date string. The format is Y-m-d H:i:s
$date = '2019-04-01 10:32:00';

//Convert it into a Unix timestamp using strtotime.
$timestamp = strtotime($date);

//Print it out.
echo $timestamp;
load more v
65%

The Task is to convert a Date to a timestamp using PHP. The task can be done by using the strtotime() function in PHP. It is used to convert English textual date-time description to a UNIX timestamp. The UNIX timestamp represents the number of seconds between a particular date and the Unix Epoch. ,Convert timestamp to readable date/time in PHP,How to convert a Date into Timestamp using PHP ?,PHP | Number of week days between two dates

Syntax:

strtotime($EnglishDateTime, $time_now)

Output:

160081920016009056001557964800
load more v
75%

$date = new DateTime();
echo $date - > getTimestamp();
40%

Step1: Convert Unix Timestamp to PHP DateTime First we will get unix timestamp and then convert it into PHP date time using PHP DateTime class.,We will cover this tutorial in easy steps with live demo to convert unix timestamp to date with PHP.,Convert Unix Timestamp To Readable Date Time in PHP,Step3: Display PHP Date Time with Specific Timezone Now we will display PHP date time from UTC time to specific timeozone.

Step1: Convert Unix Timestamp to PHP DateTime
First we will get unix timestamp and then convert it into PHP date time using PHP DateTime class.

$unix_timestamp = $_POST['timestamp'];
$datetime = new DateTime("@$unix_timestamp");
load more v
22%

How to convert epoch / UNIX timestamps to normal readable date/time using PHP 7.,Converting from epoch to normal date in PHP,Converting from normal date to epoch in PHP,Getting current epoch time in PHP

Time returns an integer with the current epoch:

time() // current Unix timestamp
microtime(true) // microtime returns timestamp with microseconds (param: true=float, false=string)
load more v
60%

Function time returns current Unix timestamp (number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).,Function microtime returns current Unix timestamp with microseconds.,There is multiple ways how to get current timestamp in PHP. ,How To Get Current Timestamp In Python

Function time returns current Unix timestamp (number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

echo time();
# 1636789637
load more v

Other "timestamp-undefined" queries related to "How do I get a unix timestamp from PHP date time?"