Uniformize date formats - how to? - php

I have two sources of information and those sources have dates in different formats.
Source A
Wed, 17 Nov 2010 12:14:10 +0000 (from a rss feed)
Source B
YYYY-MM-DD HH:MM:SS (from mysql datetime)
Request:
I would like to order them by date to retrieve only the last occurrences.
But, and to put it even more difficult, those that are stored into the database should have in consideration the TIME. Because several records could be created on the same day.
I have control over the mysql output format to choose from.
I don't have any control about the output coming from the rss feed.
This doesn't seem to be an easy achievement, and I'm wondering:
What could you suggest here?
Update:
I was thinking about:
Working source A like this:
a) creating an array for months and the specific month number
b) Retrive two chars after the first comma (or three if we count the space) (the day)
c) Convert "Nov" to a number (by using the array previously created);
d) Retrieve the 2010 (not sure how);
e) Place the year on the left, add a - place the month, and a -, place the day, add 00:00:00
At this time, they both will be equal and that could help...
But, I feel really dummy about doing all this... :s Isn't there a smart way? :)
Over - php(zend) / mysql
Thanks a lot,
MEM

If you can use PHP 5.3, I would use DateTime::createFromFormat() to normalize the date formats into a common format.
For mySQL dates:
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2010-11-17');
echo $date->format('Y-m-d H:i:s');
For the RFC 2822 format, this should work (never tested it yet):
$date = DateTime::createFromFormat('r', 'Wed, 17 Nov 2010 12:14:10 +0000');
echo $date->format('Y-m-d H:i:s');

Related

How to compare between different date formats?

I have four different date formats that I will store in a DB, Then show the latest ones.
The four different formats:
$a = '27. júní 2018 04:53';
$b = 'Friday, 09 March 2018';
$c = 'Fri, 29 Jun 2018 11:00:00 GMT';
$d = 'Mon, 18 Jun 2018 06:52:20 +0000';
They will be stored in a MYSQL Database.
What should I do with them?
Can SQL or MYSQL date type do the work?
Should I convert them using strtotime()?
Should I extract some data from specific ones to male them match?
MySQL has three data types suitable for date/time values. These are pretty well explained in the documentation.
You have three types of values:
Date alone.
Date with a time, but no time zone.
Date with a time and a time zone.
But the basic questions to ask:
Do you need just the date or is the time also necessary?
Do you need the time zone?
If you need just the date, then date will do. Otherwise, you want datetime or timetamp. Which depends on how you want to treat timezones.
If you actually need to store the timezone with the value, then I would suggest that you think hard about whether this is really necessary. Typically, timestamp is sufficient, because it stores values in UTC which can then be converted to any original timestamp. You can store the original time zone in an alternative column.
If you really need to handle all three types with no compromise, then you might need to use a string. In this case, you should use a correctly formatted string:
YYYY-MM-DD
YYYY-MM-DD HH:MI:SS
YYYY-MM-DD HH:MI:SS+TZ
The first two are readily converted to the appropriate type in MySQL. For the third, you can extract the timezone and add it as an offset (with a bit of work).
However, I doubt whether this approach is necessary. A datetime is probably sufficient.

Displaying now() data in seperate day/month/year sections?

I am using a now() code to enter just a date into the database. The date appears as Year/Month/Day (yyyy/mm/dd).
In my PHP code I have been using:
$news_date=$row['news_date'];
to recall the date on my site, I was wondering, instead of the date all showing at once, is there a way to add a separate value for day, month and year? So I could recall 'day' separate and so forth? Obviously without changing the database to have 3 different inputs, there must be an easier way? I am fairly new to PHP and MySQL so if someone could help me out with an example that would be handy! Thanks.
PHP has a very powerful function called strtotime() that intuitively converts next to any datestamp into a UNIX-based timestamp, which you can then format any way you want.
$news_timestamp = strtotime($row['news_date']);
Now you can use PHP's date() function to output this timestamp in all sorts of ways.
print 'Today is day number '.date('d', $news_timestamp); // 20
print 'The month is '.date('F', $news_timestamp); // March
print 'Post timestamp: '.date('l, F d, Y # h:ia', $news_timestamp); // Thursday, March 20, 2014 # 03:46pm

Changing date format globally

I am using PHP and mysql and using either Date or DateTime to save dates in mysql database. On site I have been displaying dates the way they are saved in database.
But now I want to show dates EVERYWHERE on site using one format:
April 17 2013
or
April 17 2013 12:20:50
I know I can use date and strtotime functions to display dates in above format. However there are a lot of places where I have date displaying code. So I am looking to automate the process where my current code works and displays dates in above format.
Any idea of how mysql trigger or some php magic could be created that converts all dates run through SELECT query automatically without changing my sql or php code since I have a lot of places in my code and it would be overkill to change code at all places?
For Example:
Date Saved in DB: 2013-04-16 12:41:26
SELECT QUERY: SELECT * FROM myTable
PHP: echo $row->dated; displays 2013-04-16 12:41:26
I want that without changing my php code, dates should be shown in above mentioned format globally on whole site.
Any ideas please how it could be achieved ?
You can directly format in via query using DATE_FORMAT()
SELECT DATE_FORMAT(myDate, '%M %d %Y %h:%i') myDate
FROM TableName
SQLFiddle Demo
and echo in your PHP: $row->myDate
MySQL Trigger doesn't project values and It is only fired during CrUD operations.
I would like to suggest you an alternative approach which i love to use.
You should use the epoch time. An epoch time is basicly the number of second that has passed since 1 January 1970
One if the benefits i love is that it is very easy to calculate
differences in time since you are just dealing with number of
seconds and not a complicated format such as sec min hrs
Another benefit is that it is very easy to store since its a
integer so you can store it in a sql db and have your php code understand it without worrying about the format and things like that.
In php, if you use the time() function, it will return the epoch time.
And if you ever want to display it in a user friendly way. you can use the following code:
$epoch = time();
$dt = new DateTime("$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00
As you can see, the format of the date is very flexible and thus easy to use.
A nice example to find the date 1 week ago:
$epoch = time() - 604800; //604800 seconds = 7 days
$dt = new DateTime("$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00

What is the time format used in facebook created date?

Hi i am working on facebook Graph API where i need all the posts information of a group. So I did it and saw [created_date'] => '2013-01-25T00:11:02+0000' what does this date and time represent i mean i know 2013-01-25 is date and 00:11:02 is time but what does T and +0000 represent.
BTW where is the server of facebook. Which timestamp should i use to match facebook time?
Thank you.
T = TIME and the +0000 is timezone offset. Facebook uses localized timezones. You can request a Unix timestamp instead of the string by adding the parameter: date_format=U to your graph API call.
Please see this link for more information.
The date format is called ISO 8601. The letter T is used to separate date and time unambiguously and +0000 is used to signify the timezone offset, in this case GMT or UTC.
That said, you generally don't need to worry so much about the actual contents; rather you should know how to work with them. To use such a date, you can use strtotime() to convert it into a time-stamp:
$ts = strtotime('2013-01-25T00:11:02+0000');
To convert the time-stamp back into a string representation, you can simply use gmdate() with the predefined date constant DATE_ISO8601:
echo gmdate(DATE_ISO8601, $ts);
Alternatively, using DateTime:
// import date
$d = DateTime::createFromFormat(DateTime::ISO8601, '2013-01-25T00:11:02+0000');
// export date
echo $dd->format(DateTime::ISO8601), PHP_EOL;
This is a standard format, specifically ISO 8601.
As much as I don't like linking to it, http://www.w3schools.com/schema/schema_dtypes_date.asp does have a good "human-understandable" explanation:
The dateTime is specified in the following form "YYYY-MM-DDThh:mm:ss"
where:
YYYY indicates the year
MM indicates the month
DD indicates the day
T indicates the start of the required time section
hh indicates the hour
mm indicates the minute
ss indicates the second
To specify a time zone, you can either enter a dateTime in UTC time by
adding a "Z" behind the time - like this:
2002-05-30T09:30:10Z
or you can specify an offset from the UTC time by adding a positive or
negative time behind the time - like this:
2002-05-30T09:30:10-06:00
or
2002-05-30T09:30:10+06:00
Therefore, in your case the +0000 indicates a time offset of 0 from UTC.

Is there a difference in the timestamp formats used by iOS and PHP?

I got some of the timestamps from the consolidated.db on my iPhone (the one from the location tracking 'scandal' recently). I made a little PHP page to convert them to nicely formatted dates then output a list, but I'm getting dates from 1980.
Do they use a different system? Or does consolidated.db have incorrect data?
Example timestamp: 316777502
My code: $date = date("t M Y", $timestamp);
I found an article that details a manual process by which it is possible to view the data:
http://dropstones.blogspot.com/2011/04/extracting-iphone-ios4-location-data-in.html
According to that article, the timestamps are not traditional timestamps of the number of seconds from 1/1/1970, but are instead based on the number of seconds from 1/1/2001 (so, there is a 31 year offset). I cannot confirm whether this is true, but if we follow the assumption that it is, we have to add the number of seconds in 31 years (978264705) to the timestamp to change it to a traditional timestamp giving the number of seconds from 1/1/1970. The line you posted would therefore be replaced with this:
$timestamp += 978264705;
$date = date("t M Y", $timestamp);

Categories