convert into a DateTime object - php

Is there a way to convert this:
Tue Aug 28 13:59:01 +0000 2012
Into a DateTime Object?
I need to diff the above value with a DateTime Object.

Just pass it into the DateTime constructor...
$date = new DateTime('Tue Aug 28 13:59:01 +0000 2012');
echo $date->format('Y-m-d H:i:s');

$date = new DateTime($yourDate);
this will covert to standard DateTime object
check php manual here . Example 2 will make it clear.

Related

Convert Time to ISO 8601 format in UTC timezone

I have date as Fri, 15 Mar 2019 08:56:57 +0000
I want to convert this date in ISO 8601 format in UTC timezone in php.
I have tried the following:
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
$output = $date->format(\DateTime::ATOM);
Is this correct?
If you want the ISO 8601 format you should use the DateTimeInterface::ISO8601 in the format method, or you can use "c":
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
echo $date->format(\DateTime::ISO8601);
// will be 2019-03-15T08:56:57+0000
echo $date->format("c");
/*
will be 2019-03-15T08:56:57+00:00
note the : in between hh and mm in offset(timezone)
generally accepted as valid ISO 8061 see:
https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
*/
Regarding the timezone if you want to force it into UCT timezone then you should use the setTimezone method on the date object first with timezone param "UTC":
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
$date->setTimezone(new DateTimeZone("UTC"));
$output = $date->format(\DateTime::ISO8601);
Note about the above that if your original date time is not in UTC(has an offset) the time will be converted to UTC and the offset will be 0000:
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0230");
echo $date->format(\DateTime::ISO8601);
// will be: 2019-03-15T08:56:57+0230
$date->setTimezone(new DateTimeZone("UTC"));
echo $date->format(\DateTime::ISO8601);
// will be: 2019-03-15T06:26:57+0000

Convert a datepicker string into simple date format in php

My vuexy datepicker returns the following string:
Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)
In my controller (Laravel) I want to convert this into simple date format - yyyy-mm-dd.
I have tried with strtotime() & date() but nothing works. It may need to use DateTime::createFromFormat() but I could not pick the syntax.
How can I convert it into yyyy-mm-dd?
This is working for me:
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$date = preg_replace('/\(.*\)$/', '', $date); # Get rid of TZ from the date string
echo date('Y-m-d', strtotime($date));
Here's another sample using the DateTime class
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$date = preg_replace('/\(.*\)$/', '', $date);
$dt = new DateTime($date);
echo $dt->format('Y-m-d');
Both Output:
2020-12-01
Hi Friend You Can Use Carbon Package one of the most recomonded package. Please follow the code as below
use Carbon; //use at the begining of class/controller
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$formated_date = Carbon::parse($date)->format('Y-m-d');
//output will be 2020-12-01

Converting Date format in PHP

I am getting a date format like Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time) form a javascript to my php function.
I want to store that to my database table in "2013-04-22 12:16:00" format.
Can any one help me to convert this date type.
I tried:
date("Y-m-d H:i:s",$startDate);
But it's giving error as
date() expects parameter 2 to be long string given
Use strtotime() and date():
$originalDate = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)" ;
$newDate = date("Y-m-d H:i:s", strtotime($originalDate));
(see strtotime and date docs on the PHP site).
or use DateTime:
<?php
$source = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)";
$date = new DateTime($source);
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
?>
DEMO
Try using strtotime to convert the timestamp to unix-format so you can use it in the date() function:
date("Y-m-d H:i:s",strtotime($startDate));
You can also try using the (usually included) DateTime class. In particular, have a look at DateTime::createFromFormat. It may help you get around ambiguities in the date string (strtotime() will sometimes fail or mis-parse a date string). DateTime::createFromFormat allows you to specifically designate the format of the date string so there can be no ambiguity.
firstly you need to save it as Timestamp so,
$startDate = strtotime($javascript_value);
$result = date("Y-m-d H:i:s",$startDate);

String to DateTime Object

I'm new at String functions, so I need a complex substr and trim functions for this string:
Wed, 28 Dec 2011 13:04:30 GMT
String comes to me always with this format. I want to convert it to DateTime object. Anybody can help me?
$dateString = 'Wed, 28 Dec 2011 13:04:30 GMT';
$dateTime = datetime::createfromformat('D, d M Y H:i:s e',$dateString);
echo $dateTime->format('d-M-Y H:i:s e');
<?php
$date = new DateTime('Wed, 28 Dec 2011 13:04:30 GMT');
echo $date->format('r');
... prints:
Wed, 28 Dec 2011 13:04:30 +0000
If you are wanting to take a date string and write that to the database as a date object using Doctrine:
Note: This is a form post example for Symfony 3 and 4.
$mynewdateobject = new \DateTime($request->request->get('mydatestring'));
Then you can write the object to the database or use it elsewhere in your code.

convert any date string to timestamp without timezone

I'm getting xml and rss feeds and putting the data into a database. I've run into two different date formats so far...
Wed, 21 Jul 2010 00:28:50 GMT
And
2010-07-20T17:33:19Z
I'm sure there will be more. My postgresql database for the date is timestamp without time zone. Is there an existing function in php or is there a procedure to convert the any date strings to timestamp without time zone (Y-m-d H:i:s)?
Use date with strtotime:
$date = date('Y-m-d H:i:s', strtotime('Wed, 21 Jul 2010 00:28:50 GMT'));
echo $date;
Result:
2010-07-21 05:28:50
.
$date = date('Y-m-d H:i:s', strtotime('2010-07-20T17:33:19Z'));
echo $date;
Result:
2010-07-20 22:33:19
You don't need to convert it at all. PostgreSQL should convert automatically:
postgres=# create table test_tz (f1 timestamp without time zone);
CREATE TABLE
postgres=# insert into test_tz (f1) values ('Wed, 21 Jul 2010 00:28:50 GMT');
INSERT 0 1
postgres=# insert into test_tz (f1) values ('2010-07-20T17:33:19Z');
INSERT 0 1
postgres=# select f1 from test_tz;
f1
---------------------
2010-07-21 00:28:50
2010-07-20 17:33:19
Timestamps are considered to be UTC.
$dt = new DateTime('Wed, 21 Jul 2010 00:28:50 GMT');
echo $dt->format('U'); // 1279672130
is the same timestamp as
$dt = new DateTime('Wed, 21 Jul 2010 02:28:50 CEST');
echo $dt->format('U'); // 1279672130
Note that the U formatting option requires PHP5.3 though. When supplying a timezone identifier in the Date String, the DateTime object recognizes the Timezone, so when you call the following on the GMT DateTime instance
echo $dt->format('Y-m-d H:i:s');
it will return 2010-07-21 00:28:50. You can change a DateTime object's timezone with it's setTimezone() method though.
$dt = new DateTime('Wed, 21 Jul 2010 02:28:50 GMT+2');
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s'); // 2010-07-21 00:28:50
But if you just need the timestamp, it's not needed.

Categories