PHP - Transform Date - php

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

Related

How to Convert Date Format in PHP

how to convert Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time) this format to mysql date format using PHP?
I tried to use strtotime but none of them working for me
date("Y-m-d H:i:s", strtotime("Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time)"));
The date fails to parse because it's in a very weird format.
If you can't control the format of the incoming date you could grab the different parts using regex and parse them:
$rawDate = "Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time)";
preg_match('/(.*?) GMT (\d+)\s\(.*?\)/', $rawDate, $matches);
$date = DateTime::createFromFormat('D M j Y H:i:s', $matches[1])
->setTimezone(new DateTimeZone('+' . $matches[2]));
echo $date->format('Y-m-d H:i:s'); // outputs 2019-07-19 06:00:00
Example: https://3v4l.org/fbuMk

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');

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.

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.

Parsing Twitter API Datestamp

I'm using the twitter API to return a list of status updates and the times they were created. It's returning the creation date in the following format:
Fri Apr 09 12:53:54 +0000 2010
What's the simplest way (with PHP or Javascript) to format this like 09-04-2010?
Cross-browser, time-zone-aware parsing via JavaScript:
var s = "Fri Apr 09 12:53:54 +0000 2010";
var date = new Date(
s.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/,
"$1 $2 $4 $3 UTC"));
Tested on IE, Firefox, Safari, Chrome and Opera.
strtotime("dateString"); gets it into the native PHP date format, then you can work with the date() function to get it printed out how you'd like it.
JavaScript can parse that date if you remove the +0000 from the string:
var dStr = "Fri Apr 09 12:53:54 +0000 2010";
dStr = dStr.replace("+0000 ", "") + " UTC";
var d = new Date(dStr);
Chrome -- and I suspect some other non IE browsers -- can actually parse it with the +0000 present in the string, but you may as well remove it for interoperability.
PHP can parse the date with strtotime:
strtotime("Fri Apr 09 12:53:54 +0000 2010");
Here is date format for Twitter API:
Sat Jan 19 20:38:06 +0000 2013
"EEE MMM dd HH:mm:ss Z yyyy"
Javascript. As #Andy pointed out, is going to be a bitch when it comes to IE. So it's best to rely on a library that does it consistently. DateJS seems like a nice library.
Once the library is added, you would parse and format it as:
var date = Date.parse("Fri Apr 09 12:53:54 +0000 2010");
var formatted = date.toString("dd-MM-yyyy");
In PHP you can use the date functions or the DateTime class to do the same (available since PHP 5.2.0):
$date = new DateTime("Fri Apr 09 12:53:54 +0000 2010");
echo $date->format("d-m-Y"); // 09-04-2010
FYI, in Java it is:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
Date d = sdf.parse(dateAsString);
sdf = new SimpleDateFormat("dd-MM-yyyy");
String s = sdf.format(d);
And in Swift:
let dateString = "Fri Apr 09 12:53:54 +0000 2010"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
let date = dateFormatter.date(from: dateString)

Categories