This question already has answers here:
Convert influxdb time format to ISO8601 format
(4 answers)
Closed last year.
With a php app, I have a date like this '2019-07-22T04:44:43.843710438Z' and I can't figure out how to convert it to a date like this 7/22/2019 3:11 PM. strtotime just returns false, I tried a few time zones and nothing works. Here is my scratch pad of stuff I've been trying
$timestamp = '2019-07-22T04:44:43.843710438Z';
//$reset = date_default_timezone_get();
//date_default_timezone_set('America/New_York');
//$stamp = strtotime($timestamp);
//date_default_timezone_set($reset);
$result = date_parse_from_format("j F Y G:i", $timestamp);
You'll have a much easier time (couldn't resist) working with the DateTime class (https://www.php.net/manual/en/class.datetime.php).
You're running into issues because the value you have isn't a timestamp so you can't use it like one.
The original time value uses nanosecond precision (9dp) which isn't supported by PHP. In order to use that time string with DateTime, you'll first have to truncate it to microseconds (6dp). It then has a method that will return your value in the desired format (https://www.php.net/manual/en/function.date.php).
$time = '2019-07-22T04:44:43.843710438Z';
$time_micro_precision = substr($time, 0, -4) . 'Z'; // drop the last three digits to move from nanoseconds to microseconds.
$datetime = new DateTime($time_micro_precision);
echo $datetime->format('j/n/Y g:i A');
Using strtotime works if you restrict the timestamp to 2 decimal places. Try the following:
$time = "2019-07-22T04:44:43.843710438Z";
$temp_str = rtrim($time, "Z");
$temp_arr = explode(":", $temp_str);
$temp_str = number_format($temp_arr[count($temp_arr)-1], 8);
$time = $temp_arr[0] . ":" . $temp_arr[1] . ":" . $temp_str . "Z";
$timestamp = strtotime($time);
echo date("m/d/Y H:i a", $timestamp);
Related
I'm having trouble calculating the number of hours worked.
We start with a time which starts as a string in this case ($time).
Then we change the time to 00:00:00 and store the result as a new variable ($newtime).
Then we need to calculate the difference between $time and $newtime but there is a formatting issue which I do not fully understand. Would anyone help?
$time = "2017-09-01 11:00:00"; //must start as a string like this
$newtime = new DateTime($time);
$newtime->setTime(00, 00,00); //change time to 00:00:00
$worktime = round((strtotime($time) - $newtime)/3600, 2);
echo "Hours Worked: " . $worktime . "<br>";
You're subtracting a timestamp with a DateTime object, so it tries to convert the DateTime object to an int, which it can't. You need to get the timestamp for the DateTime object, to subtract two ints:
<?php
$time = "2017-09-01 11:00:00"; //must start as a string like this
$newtime = new DateTime($time);
$newtime->setTime(00, 00,00); //change time to 00:00:00
$worktime = round((strtotime($time) - $newtime->getTimestamp())/3600, 2); // notice the $newtime->getTimestamp() call
echo "Hours Worked: " . $worktime . "<br>";
Demo
DateTime::getTimestamp() reference
You are mixing types (trying to cast object to int)... And maybe you didn't realize about the error you are making because you have disabled errors.
Please use, the method that Datetime class brings to you:
http://php.net/manual/es/datetime.gettimestamp.php
You can do it in both ways:
$newtime->getTimestamp()
or by using this:
date_timestamp_get($newtime)
As this:
$time = "2017-09-01 11:00:00"; //must start as a string like this
$newtime = new DateTime($time);
$newtime->setTime(00, 00,00); //change time to 00:00:00
$worktime = round((strtotime($time) - date_timestamp_get($newtime))/3600, 2);
echo "Hours Worked: " . $worktime . "<br>";
Please, be free of using this: http://sandbox.onlinephpfunctions.com/code/f78c993f709a67ac2770d78bb809e68e3a679707
My intention is to convert the following date
20/04/17 13:27:5
to this
20-04-2017 13:27:05
I tried the typical date format functions of php and also Carbon...
things like
$newDate= Carbon::createFromFormat('d/m/y H:m:s', $originalDate);
in this case
var_dump($newDate->toDateTimeString()) would bring 2019-03-20 13:00:55 which is not what I expect.
So I was not lucky....is there a way to do this in a straight forward manner?
I think this should work.
$date = "20/04/17 13:27:5";
$sec = substr($date, strrpos($date, ":") + 1);
$sec = substr("0{$sec}", -2);
$new = substr($date, 0, strrpos($date, ":") + 1) . $sec;
$newDate = Carbon::createFromFormat('d/m/y H:i:s', $new);
I changed the format since you were using m twice for "minutes" and "month". It is correct for the month, but not for the minutes. Instead use i for minutes with leading zeroes.
$sec Is what I used to get the second from the string. This gets the last position of : and will take everything after it. This assumes that you do not change the format of the string.
substr("0{$sec}", -2) Adds a zero to the current second and extracts the last two characters. That means that 50 becomes 050 and then the last two characters are 50 so we end up without the padding, but 5 becomes 05 and the last two characters are the only characters.
$new concatenates the start of the date string and the new second with the zero padding.
$newDate is your original string with the format changed.
There is issue with seconds. There must be 05 not only 5
<?php
$original_date = "20/04/17 13:27:5";
$date_explode = explode(":", $original_date);
$date_explode[2] = str_pad($date_explode[2],2,"0",STR_PAD_LEFT);
$original_date = implode($date_explode,":");
$date = DateTime::createFromFormat('d/m/y H:i:s', $original_date);
echo date_format($date,"d-m-Y H:i:s");
?>
This is a working conversion routine that creates the ISO format you are looking for. But as already mentioned you need to "fix" the strange way the seconds are specified in the original example you provide. You will have to use string functions if that really is the format you receive. Better would be to fix the code that creates such broken formats.
<?php
$input = '20/04/17 13:27:05';
$date = DateTime::createFromFormat('d/m/y H:i:s', $input);
var_dump($date->format('d-m-Y H:i:s'));
The output obviously is:
string(19) "20-04-2017 13:27:05"
Isn't it like this?
$newDate = Carbon::createFromFormat('d/m/y H:i:s', $originalDate);
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 1 year ago.
Input time format:
2016-05-03 01:38:54
Ouput time format:
2016-05-03T01:38:54.003Z
Here what is .003, is it millisecond or microsecond?
And in PHP the microsecond format is 'u', but when I am trying to use this using the following code snippet, I am getting 6 digits after the decimal point:
<?php
date_default_timezone_set("Asia/Kolkata");
echo date('Y-m-d\TH:i:s.u\Z', strtotime("2016-05-03 01:38:54"));
I am getting the output as:
2016-05-03T01:38:54.000000Z
But in this output after the decimal point it is showing 6 digit, which I want to change it to 3 digit. How can I do that?
So how should I get the output format?
date_default_timezone_set("Asia/Kolkata");
$t = strtotime('2016-05-03 01:38:54');
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );
$new= $d->Format("Y-m-d\T H:i:s.u\Z");
echo substr($new, 0, -4) ."Z";
//output 2016-05-03T 01:38:54.030Z
I'd use the DateTime class as follows:
$tz = new DateTimeZone('Asia/Kolkata');
$date = new DateTime('2016-05-03 01:38:54', $tz );
echo substr($date->format('Y-m-d\TH:i:s.u'), 0, -3) ."Z";
// 2016-05-03T01:38:54.000Z
http://ideone.com/Wrrve9
According to PHP date manual
u Microseconds (added in PHP 5.2.2). Note that date() will always
generate 000000 since it takes an integer parameter, whereas
DateTime::format() does support microseconds if DateTime was created
with microseconds.
try this
$t = microtime(true);
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );
print $d->format("Y-m-d\T H:i:s.u\Z"); // note at point on "u"
The cleanest solution might be a modified version of adityabhai at gmail dot com's comment on http://php.net/manual/en/function.date.php:
echo date('Y-m-d H:i:s' . substr((string)microtime(), 1, 4));
How do I convert 2014-10-10 04:13:24 to 2014-10-10T04:13:24+00:00 in php or mysql
The above mentioned date is for xml format in sitemaps
For reference check Q: How do I compute lastmod date? (Sitemaps.org FAQ)
code which i have tried:
echo "GIVEN DATE ".$timestamp = "2014-10-10 04:13:24";
echo "<br>";
$year = date('Y',strtotime($timestamp)).";
$month = date('m',strtotime($timestamp)).";
$day = date('d',strtotime($timestamp))."";
echo '<br>';
$hour = date('H',strtotime($timestamp))."";
$minutes = date('i',strtotime($timestamp))."";
$seconds = date('s',strtotime($timestamp))."<br>";
$gmktime= gmmktime($hour,$minutes,$seconds,$month,$day,$year)."<br>";
echo "output date".$isodate = date('c', $gmktime);
is the above out put conversion correct?
**OUTPUT**
GIVEN DATE : 2014-10-10 04:13:24
output date : 2014-10-10T06:13:24+02:00
Your output is correct in the light of the Sitemaps.org spec. "2014-10-10T06:13:24+02:00" is the same date/time as "2014-10-10T04:13:24+00:00".
Learn more about the W3C Datetime encoding which is used by Sitemaps.org.
Also, don't solve this with date functions, solve this with string function / operations: You change a single byte inside a string at a fixed position and then you append a string:
$timestamp = "2014-10-10 04:13:24";
$timestamp[10] = "T";
$timestamp .= "+00:00";
echo $timestamp, "\n"; // 2014-10-10T04:13:24+00:00
Or if you like to save some bytes in your file use "Z" instead of "+00:00" to denote the timezone:
$timestamp = "2014-10-10 04:13:24";
$timestamp[10] = "T";
$timestamp .= "Z";
echo $timestamp, "\n"; // 2014-10-10T04:13:24Z
PHP 5:
<?php
// Set the default timezone
date_default_timezone_set('UTC');
// Get Unix timestamp for a date
// Reference: mktime(hour, minute, second, month, day, year)
$time = mktime(04, 13, 24, 10, 10, 2014);
// Alternatively (must be a valid English date format)
$time = strtotime('2014-10-10 04:13:24');
// ISO 8601 date (added in PHP 5)
$isodate = date('c', $time);
echo $isodate; // 2014-10-10T04:13:24+00:00
// RFC 2822 formatted date
$rfcdate = date('r', $time);
echo $rfcdate; // Fri, 10 Oct 2014 04:13:24 +0000
?>
References:
date() / gmdate()
mktime() / gmmktime()
strtotime() : Supported Date and Time Formats
date_default_timezone_set() : List of Supported Timezones
ISO 8601
RFC 2822
In PHP
In PHP you use strptime() to parse the time and turn it into a structured array. Then pass the results of that into the mktime() function to get a UNIX timestamp.
In MySQL
Use UNIX_TIMESTAMP
SELECT UNIX_TIMESTAMP(datetime_column) FROM table;
I have a date value with time. (27/4/2014 18:00:00)
In addition to it, I have a string that consists of days/hours/mins. (10/7/0).
In the end, I need to sum the two values making one date.
In this example, the sum of the values is 37/4/2014 28:7:00.
So the desired result would be 8/5/2014 4:7:00. What is the best way to get this result in PHP??
function($date, $addon)
{
$date1 = strtotime($date);
$explode = explode('/', $addon);
$date2 = ($explode[0]*86400) + ($explode[1]*3600) + ($explode[2]*60);
return date('j/n/Y H:i:s', ($date1+$date2));
}
I'd convert them to timestamps then add them and convert them back:
$time = strtotime($time1) + strtotime($time2);
echo date('m/d/Y h:i:s a', $time);