PHP date() working sporadically - php

I am converting two strings from one date format to another but I am getting some unusual responses
The two dates i am converting are:
1: 2002-09-23
2: 2010-03-25
The PHP code is as follows for each of the dates:
1:date('d F Y', strtotime((string)$report_display->arr_output_base['Date1']['value']));
2:date("d F Y", strtotime((string)$report_display->arr_input_base['Date2']['value']));
The responses I'm getting are as follows:
1:Sometimes 31 December 1986 which is wrong but then other times I'll get 23 September 2002 which is right
2: is always 25 March 2010

When you know the format, why take risks sending it to strtotime and not use a proper method that uses the exact format?
$date = DateTime::createFromFormat('Y-m-d', '2002-09-23');
echo $date->format('d F Y');
That way there is no guesswork involved as to whether a month is a month or it is a day. That will always return the same date no matter what.

Related

PHP: Convert "13 September 2013 - 23:55" to SQL Server formatted datetime format

Trying to get my head around this, but can't seem to figure it out. I'm using PHP and attempting to convert the user-submitted date/time selection which outputs:
13 September 2013 - 23:55
I would like to convert that to the standard SQL Server format like:
2013-09-13 23:55:00.000
I've messed with the PHP strtotime() function sending it only the "13 September 2013" part, but it only outputs a long (seemingly) random number.
Is there any easier method for this?
Have a go with:
$date = DateTime::createFromFormat('d F Y - H:i','13 September 2013 - 23:55');
echo $date->format('Y-m-d H:i:s');
This lets you specify a format to read from.
strtotime returns the unix timestamp, you need to turn it to date string.
php > echo date('Y-m-d H:i:s', strtotime('13 September 2013 23:55'));
2013-09-13 23:55:00
You have to make two separate functions,
for converting month to numeric
year to two digit number
and after that you can break the user input to its desired three parts.

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.

strtotime() always returns same day of month but gets other parts of date correct

I have a part of a simple web app that takes input from a JavaScript calendar picker, sends it to the server, and then the server converts it to a human readable time and echos it back out.
My HTML form ends up having a value formatted as MM/DD/YYYY.
When this gets POSTed to the server this PHP transforms it into a differet format (please note that I'm using CodeIgniter so $this->input->post() is the same as $_POST[]):
php
$date = date('l, F n, Y', strtotime($this->input->post('date')));
Example input and output
HTML text input will get a value of "04/21/2013".
PHP's strtotime() will echo back "Sunday, April 4, 2013".
No matter what date I put in there, strtotime() always gives me the correct date back except for the day of the month which always ends up being the same number as the number of the month (for example, any dates in May become "May 5, 2013" and so on).
Update: Solved
As soon as I posted this I realized it was the 'n' in 'l, F n, Y' that caused the issue. Turning it to a 'j' fixed things. Sorry to waste everyone's time.
Use j for day of the month, not n which is the numeric month:
php $date = date('l, F j, Y', strtotime($this->input->post('date')));
See it in action
See the documentation for date() here: http://php.net/manual/en/function.date.php
Change the n in the first parameter of your date function to j and you will get the number of the day of the month.

Convert time from JSON string with PHP

I have an issue with converting time string I get from JSON to another format. Somehow the date is set to minus 24 hours.
Here's object from JSON
[date] => 2011-07-02T00:00:00+02:00
I'm using strtotime() and date()
date('l, d F Y', strtotime($day->date));
But the output looks like this
FRIDAY, 01 JULY 2011
Obviously the date in JSON is Second of July. Does anyone have any idea why this happens? Am I missing something important? Will really appreciate any help!
I think you should use DateTime. It does not depend on hosts TimeZone. Beside the format is valid ISO8601. So DateTime would have not problem at all.
$dt = new DateTime("2011-07-02T00:00:00+02:00");
echo $dt->format("l, d F Y");
// Echos Saturday, 02 July 2011
http://ideone.com/yPp4d
PHP doesn't understand an infinite array of time/date strings. What is 'obvious' to a human, is not so obvious to a computer. Without a specific parser for that exact date format, how is the computer language to understand what the T in your example is for??
PHP strtotime formats will show you what formats PHP can convert a string from, to a time or date object.
Even as the date/time is parsing correctly, your tzcorrection of +0200 is telling PHP to correct for a timezone difference of GMT + 2 hours, which is likely not your correct timezone offset and thus giving you the error.

Categories