String to DateTime Object - php

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.

Related

php date() function print formatted date

I would like to print a date format like: Sun, 08 Mar 2015 14:54:54
I use the function date("r"); but the output is: Sun, 08 Mar 2015 14:54:54 +0100
how to remove part +0100?
Use the right parameters:
echo date('D, d M Y H:i:s');
Use DateTime class and it's that easy:
$date = new DateTime();
echo $date->format("D, d M Y H:i:s");
Output:
Sun, 08 Mar 2015 15:00:57
Use following.
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
echo date('D, d M Y G:i:s');
// Prints something like: Sun, 08 Mar 2015 14:30:10
Read more here

Formatting date and time to get date, month, year each in seperate var

In my json response of twitter API I get time stamp like this
Thu Mar 13 14:24:13 +0000 2014
I tried to format in this way:
$created_at = $thing->created_at;
$date = DateTime::createFromFormat('D M d H:m:s O Y', $created_at);
echo $created_at;
echo $date->format('H:m:s');
Which gives result like this:
Thu Mar 13 14:24:13 +0000 2014
2015:12:13 //formated result. How come 2015?????
Wed Mar 12 14:18:14 +0000 2014
2015:06:12
Tue Jan 21 12:50:17 +0000 2014
2018:02:21
Thu Dec 12 09:29:16 +0000 2013
2015:05:12
Why giving wrong result?
I want to get month, year in seperate variable.
You can simplify the creation of the DateTime by doing this:
$dt = new DateTime('#' . strtotime('Thu Mar 13 14:24:13 +0000 2014'));
This parses the date string to a Unix timestamp, and then creates a DateTime object.
echo $dt->format('Y-m-d H:i:s'); // yields the correct result.
You are using month format character m instead of minutes i, thats why you get "wrong" output.
$dt = new DateTime('Thu Mar 13 14:24:13 +0000 2014');
echo $dt->format('H:i:s');

Using PHP strtotime() function

I'm getting an external RSS Feed Post in different website, what I noticed is that they all have a different format of Publish Date.
Some uses a formatting like this:
2012-04-09T08:23:07Z
Fri, 06 Apr 2012 01:25:43 0000
There are still lots of format but I was just showing you two examples.
Now, I want everything to have a uniform format, so i'm using strtotime(). Here's my code:
$date = "2012-04-09T08:23:07Z";
date( "F d, Y", strtotime( $date ) );
/* Result: April 09, 2012 */
My problem here is the second format Fri, 06 Apr 2012 01:25:43 0000
There is a 0000 at the last which makes the strtotime() act weird.
$date = "Fri, 06 Apr 2012 01:25:43 0000";
date( "F d, Y", strtotime( $date ) );
/* Result: April 06, 0000 */
Obviously the result should be April 06,2012.
My question here is that how can I not let strtotime() act weird when it detects a format that has a 0000 at the last. Any idea how to do this? Your help would be greatly appreciated! Thanks! :)
Thanks!
I'm pretty sure you have a formatting issue:
Fri, 06 Apr 2012 01:25:43 0000
Notice the two spaces before 0000? That's not a coincidence. The original is probably this:
Fri, 06 Apr 2012 01:25:43 +0000
Which is generated with gmdate(DATE_RFC822). In URI's, the + symbol often gets translated to a space. I think that's your problem.
How about just stripping the "0000"? Something like:
$date=str_replace("0000", "", $date);
try this:
<?php
$date = "Fri, 06 Apr 2012 01:25:43 0000";
if (strtotime($date) !== false)
{
$timestamp = strtotime($date);
echo date( "F d, Y", $timestamp );
}
else
{
echo "error";
}
?>
for $date = "Fri, 06 Apr 2012 01:25:43 0000";
outputs error.
$date = "Fri, 06 Apr 2012 01:25:43";
gives output.

PEAR Date date_create_from_format

Which PHP function do I use to parse a date that is in the format "Fri, 28 Jan 2011 17:57:37 GMT"
strtotime doesn't seem to work for me on that date.
Thanks.
$date = date_create_from_format('D, j M Y H:i:s O', 'Fri, 28 Jan 2011 17:57:37 GMT')
Full docs on the format characters here.
Beats me why couldn't look this up yourself - you've got the function name in your post's title.

PHP - Transform Date

I have the following date and time in an RSS feed:
Fri, 01 Oct 2010 12:26:57 +0000
However I just want to display:
Fri, 01 Oct 2010 12:26:57
There should be a really simple way to do this in PHP, right?
If you don't mind showing the date in UTC/GMT (I forget which), then just use substring to strip off the +0000. However, if you want local time, you'll have to convert the string to a timestamp and then format the timestamp back to a date string.
$uncleandate = 'Fri, 01 Oct 2010 12:26:57 +0000';
$timestamp = strtotime($uncleandate);
$cleandate = date('D, d M Y H:i:s', $timestamp);
$clean_string = str_replace(" +0000", "", $your_date_string); should do the job
see str_replace doc

Categories