get time in utc 2015-03-19T10:57:40.174Z format - php

I am trying to get time in 2015-03-19T10:57:40.174Z format but its not working for me
I have tried these-
1.
echo date('Y-M-DTHHM:SS.MMMZ');
2.
date_default_timezone_set('utc');
$datetime = new DateTime();
$str = $datetime->format(DateTime::ISO8601);
echo $str;
output-2015-03-19T17:27:46+0530
3.
date_default_timezone_set();
print date('c');
output-2015-03-19T17:33:52+05:30
but i need exact format(2015-03-19T10:57:40.174Z). how can i do that ?
please help.

Related

Converting Time with timezone PHP

I'd like to convert value/date/time that I got from callback
raw value that I got is like this
$value='2021-01-20T19:03:52.355+0300';
I need to convert it into like this
$value='20-01-2021 23.03.52,355000 +07:00';
what I've done some substr and concat
but unfortunately it ends up with string and my db datatype format is timestamp and i can't insert the value to db
read some about DateTime::createFromFormat
and I can convert the time format but still no clue for converting to another timezone
You could change the timezone using setTimezone() :
$value = '2021-01-20T19:03:52.355+0300';
$expected = '20-01-2021 23.03.52,355000 +07:00';
$datetime = new \DateTime($value);
$datetime->setTimezone(new \DateTimeZone('+0700'));
var_dump($datetime->format('d-m-Y H.i.s,u P') == $expected); // bool(true)
You can try this code, this will work for you and you can set the timezone as per your need.
// Input : '2021-01-20T19:03:52.355+0300';
// Output : '20-01-2021 23.03.52,355000 +07:00';
date_default_timezone_set('Europe/London');
$datetime = new DateTime('2021-01-20T19:03:52.355+0300');
// timezone to convert.
$la_time = new DateTimeZone('Asia/Krasnoyarsk');
$datetime->setTimezone($la_time);
echo $datetime->format('d-m-Y H.i.s,u P');
Output:
20-01-2021 23.03.52,355000 +07:00
you can do like this
$datetime = new \DateTime('2021-01-20T19:03:52.355+0300');
$datetime->setTimezone(new \DateTimeZone('+0700'));
date_format($datetime, 'd-m-Y H.i.s,u P');

Convert date and time to only year in php

I have a variable is which the value coming is Date along with time in php. How do I convert it into a variable to get only the year? I do not need automatic updation but the format change is needed. Normal answers are giving it about date but my variable is containing time as well.
The format coming by now is 2017-12-11 4:06:37 and i need only 2017
Use like this:
<?php echo date('Y',strtotime('now'));?>
You can you simple DateTime function and date_formate() function for displaying separate year, month and date.
For that you have to first convert in Object of your current Date time string by using :
$date = new \DateTime('2017-12-11 4:06:37');
And then you can use date format function by using below code:
echo date_format($date, "Y"); //for Display Year
echo date_format($date, "m"); //for Display Month
echo date_format($date, "d"); //for Display Date
You can code like this (working perfectly):
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y') . "\n";
As mentioned by Himanshu Upadhyay, this is correct and the easiest way.
<?php
echo date('Y',strtotime('now'));
?>
But i would recommend you to read this here. You should really do actually!
By using DateTime class
$date = new \DateTime('2017-12-11 4:06:37');
echo $date->format('Y');

create altered time record based on existing value

How do you take an existing 'date and time' value and convert it to the same date but a specified time?
For example, $time="2017-09-01 13:18:00" -> how to do you convert to "2017-09-01 23:59:59"? It must keep the date but change the time to 23:59:59.
You can do it like this by explode and simple concatenation
<?php
$time="2017-09-01 13:18:00";
$date = explode(" ", $time)[0];
echo $date." 23:59:59";
?>
Live demo : https://eval.in/853822
Update
I think you need this
<?php
$date = new DateTime('2017-09-01 13:18:00');
$date->setTime(23, 59,59);
echo $date->format('Y-m-d H:i:s') . "\n";
?>
Live demo : https://eval.in/853857
What about this?
$date = new DateTime('2017-09-01 13:18:00');
$date->add(new DateInterval('PT10H30S'));

DateTime input convert between timezones in PHP

I have an input box that grabs local time
date_default_timezone_set('America/Los_Angeles');
echo "<input type='datetime-local' name='fromDate' class='dates'>";
When I enter 12-31-2014 10:00:00 PM in the input
echo $_POST['fromDate'];
Response: 2014-12-31T22:00:00
$test = new DateTime($_POST['fromDate']);
echo $test;
I get 2014-12-31T22:00:00 America/Los_Angeles
Then when I convert
$from_dateGMT = new DateTime($_POST['fromDate'], new DateTimeZone('Europe/Paris'));
$from_date = $from_dateGMT->format('Y-m-d\TH:i:s');
echo $from_date;
I get 2014-12-31T22:12:00 UTC, which is the same time listed above and should be adding 8 hours.
What am I doing wrong?
I don't deal with dates/times in PHP ever, so this is a learning experience for me.
Perhaps this will work
$test = new DateTime($_POST['fromDate'], new DateTimeZone('America/Los_Angeles'));
$test->setTimezone(new DateTimeZone('Europe/Paris'));
echo $test->format('Y-m-d\TH:i:s');
At least that is how it is done in php manual here: http://us2.php.net/manual/en/datetime.settimezone.php

How do you change the timezone in PHP for an existing timestamp?

The code for the date and time function:
function date_and_time($format,$timestamp) {
$date_and_time = date($format,$timestamp);
return $date_and_time;
}
And then the code to display it:
<?php
echo date_and_time("dS F Y", strtotime($profile[last_activity_date_and_time]));
?>
The value of $profile[last_activity_date_and_time] is 2010-01-18 14:34:04
When displayed it shows up as 18th January 2010 - 02:34pm
But, is there any way to change the timezone it is displayed in?
Not sure if this what you're looking for, but try DateTime
date_default_timezone_set('Europe/London');
$datetime = new DateTime();
$datetime->setTimestamp($yourTimestamp);
echo $datetime->getTimezone()->getName();
echo $datetime->format(DATE_ATOM);
$la_time = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($la_time);
echo $datetime->getTimezone()->getName();
echo $datetime->format(DATE_ATOM);
You can use the this function to set default time zone:
date_default_timezone_set('Europe/London');

Categories