This question already has answers here:
PHP DateTime microseconds always returns 0
(18 answers)
Closed 6 years ago.
I have a timestamp
1457459333506 (Tue, 08 Mar 2016 17:48:53 GMT)
This should be Unix timestamp with milliseconds. I want to write this as a string, so I'm using
$dt = new DateTime("#$unixTimestamp");
echo date('Y-m-d H:i:s.u', $unixTimestamp) . "<br>";
But the output is 2016-06-08 22:09:22.000000
This is obviously wrong, and has no milisecond precision. So I've tried
echo date('Y-m-d H:i:s.u', $unixTimestamp / 1000) . "<br>";
Which outputs as 2016-03-08 17:48:53.000000 (correct, but also has no milisecond precision).
How can I get this to output correctly as: 2016-03-08 17:48:53.506 ?
As simple as
$unixTimestamp = 1457459333506;
$dt = DateTime::createFromFormat("U.u", $unixTimestamp / 1000);
var_dump($dt);
with a DateTime object
Demo
Related
This question already has answers here:
Using strtotime for dates before 1970
(7 answers)
Closed 4 years ago.
date('m/d/Y', strtotime('7-Jan-69'))
It gives output as 01/07/2069, Where
date('m/d/Y', strtotime('7-Jan-75'))
This gives output as 01/07/1975, Why is so and what is the catch?
From the docs:
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)
Any date before 1970 will be understand as date after 1970
do you need something like this?
<?
// function to convert string and print
function convertString ($date)
{
// convert date and time to seconds
$sec = strtotime($date);
// convert seconds into a specific format
$date = date("Y-m-d H:i", $sec);
// append seconds to the date and time
$date = $date . ":00";
// print final date and time
echo $date;
}
// Driver code
$date = "06/12/2014 04:13 PM";
convertString($date);
?>
To fix that you can use DateTime instead of strtotime() like below,
<?php
$date = $dt = new DateTime('7-Jan-75');
echo $date->format('m/d/Y');
?>
Reason for not working in your case with strtotime:
If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999.
See the notes below for possible differences on 32bit systems
(possible dates might end on 2038-01-19 03:14:07).
DEMO: https://3v4l.org/d8eoK
This question already has answers here:
How to convert a 13 digit Unix Timestamp to Date and time?
(3 answers)
Closed 4 years ago.
I am attempting to convert an epoch timestamp value to a formatted date string in PHP and getting unexpected results:
$dateStart = 1104555600000;
$formattedDate = date('Y-m-d', $dateStart);
Expected Result: 2005-01-01
Actual Result: 1993-09-26
1104555600000 is equivalent to 2005-01-01
However, $formattedDate ends up being 1993-09-26
Actually you epoch timestamp is in millisecond, convert it to second and then convert it to date
<?php
$dateStart = 1104555600000;
echo $formattedDate = date('Y-m-d', $dateStart/1000);
Output:- https://eval.in/981417
This question already has answers here:
PHP, How to get current date in certain format [duplicate]
(3 answers)
Closed 6 years ago.
I want to get current date and time in certain format .
and current timestamp minus 15 minutes in certain format .
How do I do this in PHP?
Please try following code :
echo "current time: " .date('Y-m-d h:i:s');
echo "<br>current timestamp minus 15 minutes :". date('Y-m-d H:i:s', strtotime('-15 minutes'));
You could get current datetime using php date() function, for your format you could use following code -
date("Y-m-d H:i:s")
for current time stamp minus 15 minutes you could use time() function -echo time() - (15 * 60)
to get exact time of minus 15 minutes in datetime format you could use this code - echo date("Y-m-d H:i:s", time() - (15 * 60));
Current time:
date_default_timezone_set('Australia/Melbourne');
$date = date('m-d-Y h:i:s a', time());
-minus 15 minutes:
echo date('m-d-Y h:i:s', strtotime('-15 minutes'));
Try and google next time :)
This question already has answers here:
Using strtotime for dates before 1970
(7 answers)
Closed 9 years ago.
I am trying to separate time from the given string date & time. I simply tried with following example.
$time = '2014-01-20 10:45:45';
echo '<br>'.$finalTime = date("H:i:s",strtotime($time));
echo '<br>'.$date = date("d F Y",strtotime($time));
I am getting correct date and time.
10:45:45
20 January 2014
But when I tried with given string, no correct result.
$time = '1899-12-30 19:30:00';
echo '<br>'.$finalTime = date("H:i:s",strtotime($time));
echo '<br>'.$date = date("d F Y",strtotime($time));
PHP is always returning me following result.
00:00:00
01 January 1970
I am not sure whether is there any limitation on date function that is not returning 1899. Is that so?
Your date is before the unix epoch. DateTime() allows you to work around that.
$dt = new DateTime("1899-12-30 19:30:00");
echo $dt->format("d F Y");
echo $dt->format("h:i:s");
Use DateTime and DateTime::format():
$time = '1899-12-30 19:30:00';
$dt = new DateTime($time);
echo $dt->format('d F Y H:i:s');
Working example: http://3v4l.org/fM22Z
The strtotime() method is limited by the Unix epoch, which is Jan 1, 1970.
Update: As Mark comments below, your code would work all the way back to 1901 (as a negative timestamp), see here: http://3v4l.org/CSJte
This question already has answers here:
PHP turn PM and AM into 24 hour
(3 answers)
Closed 8 years ago.
If I have hours in the following format :
2:30 am how can I turn it into :
14:30:00
and if I got the date 2013-07-17 and the time 14:30:00 how can I turn it to a datatimestamp ?
for example this will produce : 1374053400
thanks.
am is before noon, not after..
This is what I would do:
$timeString = '2:30 am';
$dateString = '2013-07-17';
$timeZone = new DateTimeZone('Europe/London');
$date = new DateTime($dateString . ' ' . $timeString, $timeZone);
echo $date->format('H:i:s'); // 02:30:00
echo $date->getTimestamp(); // the timestamp as int
Have you tried to search in this site? :)
The second question has a really-similar one here:
How to convert date to timestamp in PHP?
As for the first one, the answer is in the official php documentation:
http://php.net/manual/es/function.date.php