Issues with PHP strtotime function rolling back one month - php

I have a date stored in MySQL as 2012-11-00 and I am trying to convert it so I can format it properly on a PHP page. When I use the following I always end up getting one month behind. So if the date in MySQL is 2012-11-00 then the result from PHP is Oct 2012.
date("M Y", strtotime($resVal['dateField']))
Any thoughts as to why it does this?

#1.
Why dont you have dates saved as 2012-11-01?
Because 2012-11-00 is "invalid", so for PHP that date is equal to 2012-10-31.
#2.
Use DateTime::createFromFormat which can handle your (strange) format with ease:
echo DateTime::createFromFormat('Y-m-|??', '2012-11-00')->format('M Y');
will return Nov 2012
#3.
Format date in MySQL with DATE_FORMAT function:
SELECT DATE_FORMAT(date_field, '%b %Y')
FROM table

Save the date as "2012-11-01", since 2012-11-00 is not valid.

2012-11-00 is not a valid date
2012-11-01 is a date...
So probably it is rounding to 2012-10-31

Related

RFC-822 DateTime Formatting in PHP with Database [duplicate]

This question already has answers here:
Conversion from MySQL date to RFC822 date format
(2 answers)
Closed 9 months ago.
I have a date field on my database. the date format as following.
June 17, 2013
Im using the format as
date("F j, Y");
So my question is there a way that i can display this date in RFC-822 format using php? or do i need to start saving the date in RFC-822 format from now on? Thanks in advance.
Using the following syntax, you can display current time in RFC822 Format.
$date = new DateTime('2000-01-01');
echo $date->format(DateTime::RFC822);
Neither.
From now on you have to start using format supplied by database.
You have to understand the difference between storage format and display formatting. It's different matters. When storing data in mysql, you have to follow mysql rules. So, instead of June 17, 2013 you have to store 2013-06-17.
And then convert at output to whatever format required - not limited to a single one but whatever format is demanded by destination.
None of the other answers worked for me, so this is what worked... to take a date in PHP and output it in RFC822:
date("D, d M Y G:i:s T", strtotime($date));
Hope that helps others.
As was pointed out your best bet is to change the way you are storing your dates to something other then a string. date("Y-m-d", strtotime($date)) can assist you in this endeavor.
But to solve the immediate need you can utilize use strtotime, date and the DATE_RFC822 constant to get you what you are looking for.
echo date(DATE_RFC822, strtotime($value));
See First example on php date documentation
As #ashleedawg and others mentioned in some comments the simplest solution that works:
date("D, d M Y H:i:s O", strtotime($date));
Mind the "H" and the "O" ;)
Thanks!
If you want to date format something in PHP for RFC-822 , then just do this...
date('r', strtotime($date))
'r' ยป RFC 2822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200
Source: PHP.net: DateFormat
But, as other stated, you don't want to store this in your database! However, you'll need to use r for other things, like XML-RSS date time formats...
All date-times in RSS conform to the Date and Time Specification of RFC 822... (Source: RSS 2.0 Specification.)
date_format(date(your database field), '%D, %j %M %t')
and what type of format you want just see the link
date and time format for Mysql
You can save it as TimeStamp in database and show it RFC822 format
date(DATE_RFC822, time());
This is the only solution that worked for me:
date("D, d M Y H:i:s T", strtotime($date));
Other examples above that didn't work include using the DATE_RFC822 format specifier, which puts out a 2-digit year, instead of 4 digits. Then the other suggestion to use G:i:s for time doesn't work because G specifies no leading zeroes, so you'll get 2:00:00 instead of 02:00:00.
don't use T at the end but an "O", it works for me

Strtotime won't convert my date accurately

I know this is a common problem, but I can't seem to find the solution anywhere. In my last question [complicated date functions - comparing, subtracting I needed to compare timestamps to get an accurate date for some Cisco logs.
The best I can come up with (since the dates don't actually feature the year) is in the format
Mar 1 2013 00:03:55:
from
Mar 1 00:03:55:
But when I ran some tests, strtotime is converting this date as
Jan 1970
Using
print date("M Y", strtotime($c_log))."\n";
Am I going to have to reformat it into a date it can understand? I don't appear to have the DateTime function. What's the simplest way?
Use the date_parse_from_format() function so you can specify the format
http://php.net/manual/en/function.date-parse-from-format.php
Works for me:
echo date('M Y', strtotime('Mar 1 00:03:55')); // outputs Mar 2013
it should default to the current year when one isn't included.

How to select date and time from oracle date field using php date function

How to take the time from date stored as 12/25/2012 5:12:05 AM .
date('l F j, Y, g:i a',strtotime($last_login_details[FL_DATETIME]));
This above function returned time as 12:00 am which should return 5:12AM.
FL_DATETIME has datatype DATE.
On database, the value is being stored like this :
12/25/2012 5:12:05 AM
According to the docs - http://docs.oracle.com/cd/B19306_01/server.102/b14220/datatype.htm#i1847 -
For input and output of dates, the standard Oracle date format is DD-MON-YY
That is most likely why $last_login_details[FL_DATETIME] is echoing 25-DEC-12
Try changing your query using TO_CHAR()
SELECT TO_CHAR(FL_DATETIME, 'MM/DD/YYYY HH24:MI:SS A.M.') AS FL_DATETIME ...
see http://infolab.stanford.edu/~ullman/fcdb/oracle/or-time.html#date format
Solved my problem by :
SELECT TO_CHAR(FL_DATETIME, 'DD.MM.YYYY:HH24:MI:SS') FROM "FMS_LOG"
First of all, in my opinion, you should be storing all dates as unix timestamps. This makes it lot easier for you to do searches against times, and removes any inconsistencies that may arise from date string manipulation.
Second, I tested your code; it looks to be OK from what I can tell. Echo out what you are getting in the $last_login_details[FL_DATETIME] variable. The issue may lie in the variable assignment, and not the date string manipulation.
Hope that helps!

Converting an MySQL Date to friendly string.

I have a date stored in a MySWL database in the following format.
2011-08-23 00:00:00
I'm then pulling that date in with php like so
echo $row['start'];
I want to edit the PHP code to display the date in the following format.
08/16/2011 12:00 am
Is there an easy way to do this?
I prefer:
echo date('m/d/Y g:ia',strtotime($row['start']));
Parse the timestamp stored by MySQL with strtotime to a unix timestamp, then use the date function to format it.
echo date('m/d/Y g:ia', strtotime($row['start']));
date_format is your friend.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
select date_format('2011-08-23 00:00:00','%m/%d/%Y %I:%i %p') -- 08/23/2011 12:00 AM

How to make date time like this?

Hi I am saving data from rss feed url. From that me got date time like this.
Sun, 2 January 2011 03:04:02 GMT+5:30
How to change this date to this format 2nd January 2011, 03:04 PM using php?
any body knows the solution please help me.
You can se the strtotime function to convert the existing string and the 'r' specifier to the date function as follows (looks like you want it in RFC 2822 format, if not tweak accordingly):
date('r', strtotime("Sun, 2 January 2011 03:04:02 GMT+5:30"));
Incidentally, make sure you're setting your local timezone correctly via date_default_timezone_set, etc.
The following functions are useful for taking a string and getting a timestamp back:
strtotime()
DateTime::createFromFormat()
After you have it as a timestamp, you can reformat it using date(). I'm not 100% sure if strtotime() would accept that format, but it should accept it because the format it isn't ambiguous.
echo date("js F Y, h A", strtotime($oldDate));

Categories