Converting MySQL datetime to RFC 2822 Using PHP date() Function - php

I'm setting up PHP's if-modified-since header on our webpages. I can detect if a php file has been saved and then properly set the header using PHP's filetime() and date() functions. However I also need to properly set the header when information in the database that gets displayed on the php page changes.
For the file being changed I use PHP's date() function like so:
date("r", $file_mod)
However when I use it on what I get from querying a MySQL datetime field, I always get the following response where the date, time, year, and month are wrong:
Wed, 31 Dec 1969 18:33:30 -0600
What is the proper way to convert from MySQL's datetime to RFC 2822, or at least how do I convert to a format to put into PHP's date() function to get the correct response?

SELECT DATE_FORMAT(exampledate,'%a, %d %b %Y %T') // given a MySQL datetime
date('r') // for PHP

You might consider using MySQL's date_format function in your query. That way the date will arrive in the correct format.

Check youre timezone settings
http://php.net/manual/en/function.date-default-timezone-set.php
functions like date() depend on these settings.

strtotime will do the job
If you don't mind, I'd like to see your implementation of "if-modified-since". I need to do that on one of my sites.

Related

How to get '2017-08-29T09:43:42.335Z' date format in PHP?

How can I get 2017-08-29T09:43:42.335Z date format in PHP.
And what does T09:43:42.335Z correspond to?
T09:43:42.335Z is the time (including milliseconds) in the Zulu timezone (the T is just a separator).
This is not a time format that's built-in in PHP. As of PHP 7.0.0 you could make it yourself like so:
$date->format('Y-m-d\TH:i:s.v\Z');
In earlier PHP versions there is no "milliseconds" option, so you would have to concatenate that yourself.
The Zulu timezone is equal to UTC+0. Make sure you convert your time to UTC+0 before you print this out, the format command won't do that for you.

Timezone not changing after using date_default_timezone_set

I'm setting my default time zone for my page using:
date_default_timezone_set("America/Los_Angeles");
I have to set it there, because my server doesn't allow me to alter the php.ini file or .htaccess. The problem is, when I use this:
NOW()
to send the current time to my database, it still send it as the UTC timezone.
What I'm trying to do is display comments users display from a comment box on the page, and it's showing the time for each of the comments in the wrong timezone now.
date_default_timezone_set is a PHP function. It can only affect the behaviour of PHP.
NOW() is a database function, and changing your timezone in PHP has no effect on it. NOW() returns in the format YYYY-MM-DD HH:MM:SS.
time() is the equivalent PHP function. time() returns simply the number of seconds since the Unix Epoch. To get output in the same format as NOW(), use date("Y-m-d H-i-s");. This automatically uses time() underneath to get the current system time.
Read more:
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_now
http://php.net/time
http://php.net/date
From the manual
date_default_timezone_set — Sets the default timezone used by all date/time functions in a script
Thus, use the date_default_timezone_set function only affects those functions. You should instead use time() instead.

Converting 7 Aug 2012 to php date format

I'm creating a mobile version of a website. On the normal version we just use a jquery date picker to select the date. I haven't added anything like this yet on the mobile version because It seemed my Iphone was smart enough to realise it was a date field and give you date scrollers.
But after I've selected the date it's coming up at 7 Aug 2012 which raises a few questions for me... is this standard? or am I going to have to code for different date formats coming from different phones?
Would it be best to use a jquery date picker? (I'm not sure this would be very tidy).
What's the best way to convert a date like this into a format that php and my database can handle. I have done a bit of looking around google but haven't found much as of yet.
Try this
<?php
$today = date('Y-m-d', strtotime('7 Aug 2012'));
echo $today;
?>
gives 2012-08-07
You can change its output by altering Y-m-d in date()
Refer this!
$date = DateTime::createFromFormat('j M Y', '7 Aug 2012'); is the most robust way to do it if you know the format. strtotime guesses which isn't necessarily a good thing, and you won't be informed (i.e. an exception won't be thrown) if the date is in a bad format.
See the manual here: http://www.php.net/manual/en/datetime.createfromformat.php
I don't think the date format is standard. It is very probable that it is affected by the user settings (eg. locale). Eg. if the user defines a Portuguese locale I think the date would be a little bit different.
Using jquery date picker you have the advantage of being able to specify your desired format (applying the dateFormat option). Then you could safely write a PHP script that reads the date in your defined format. You can use PHP's date() function as specified by #Bhuvan Rikka.

How do I force strtotime() to return date instead of time when given ambiguous $time?

Good evеning!
echo date('r', strtotime('10.01.11'));
Prints: Sun, 05 Feb 2012 10:01:11
Expected: Mon, 10 Jan 2011 00:00:00
How do I force strtotime() to parse the input string as a date only? I have to convert a bunch of dates in different format. DateTime::format is not an option since I don't know all the formats the script will run into, and it's not even installed on the server (and i don't have privileges to do it).
Tried
strtotime('day 10.01.11'),
strtotime('10.01.11 00:00:00'),
strtotime('10.01.11 midnight')
- nothing worked.
Any help is much appreciated
How do I force strtotime() to parse the input string as a date only?
You don't. strtotime uses very well-defined parsing formats. What it generates will depend entirely on what you give it.
'10.01.11' is parsed as a time format, as it will always interpret three pairs of digits separated by periods as a time. It will recognize dates when separated by dashes, slashes or spaces. Annoyingly, there's an example there on the date format page that uses dots, but there doesn't seem to be a sure-fire way to force date parsing instead of time parsing. Sigh, PHP.
If you need that specific format to be interpreted as a date instead of a time, you have two options.
First, you can use a different date parsing method. If the expected format never changed, you could use DateTime::createFromFormat() or the horrifying strptime. You've indicated in comments that the format will vary and your PHP version is old enough not to have DateTime, so this might not work for you.
Second, you can pre-process the data. At least in this example, a conversion of . to / may do the trick, though 10/01/11 can be ambiguous as a date to humans. There's nothing wrong with a little regex sniffing to determine how to best process data.
There's also a third option: if you're getting this information from users, make your application begin forcing users to enter dates in a normal, consistent, parseable format. It may take some time to train your users to use YYYY-MM-DD, but it's probably the most sane long-term bet.
Is modifying the input an option for you?
$str = '10.01.11';
$str = str_replace('.', '/', $str);
echo date('r', strtotime($str));
However, this will still output Sat, 01 Oct 2011 00:00:00, according to the MM.DD.YY pattern (US standard).
EDIT: Depending on you usage, you might consider creating a list of regex patterns and parse the date accordingly. It is very hard to make a code like this to be open to all possibilities.
Tested, this works:
$date = DateTime::createFromFormat('d.m.y', '10.01.11');
echo $date->format('r');
http://codepad.viper-7.com/OH7Kyn
why don't you add the time set to 00:00:00 by default?
e.g.
echo date('D, d M Y H:i:s', strtotime('10.01.11'));
also strtotime uses the american date format so this will be translated into 1st of october 2011. it's easier to use the iso date format
I don't think strtotime knows how to parse those dates. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates.

time() to DateTime

I have an application that uses time() to record the time a topic was posted. I have done this for a long time and the only glitch i ever had with the method was that the time was always off by an hour (mainly a DST issue i never looked into).
I'm want to switch to the DateTime method, since I'm also switching to Twig, which uses that date format when setting a timezone.
But from what I can see, you can't use timestamps to parse the date. my question is, how do you input a date and parse it and what format are they looking for if it isn't time()?
By the sounds of it your server's default timezone is UTC. I suggest you try changing the default_timezone setting in php.ini to something that applies DST. This will mean that when you use date(), the resulting output will be correctly adjusted for DST.
you can't use timestamps to parse the date
There is a way, if your php version is >=5.3 :
$time = time();//or other time
$date = DateTime::createFromFormat('U',$time);
but as a matter of fact it is somewhat weird to use timestamp as DateTime does not rely on it. You can parse a date string directly with the datetime constructor if your date is english or "french" (i.e d/m/y). You can also create your custom format.
how do you input a date and parse it and what format are they looking for if it isn't time()
if it is a a standard format date :
$date = new DateTime($yourString);//would throw an exception if the format is wrong
if it is a custom format, like DD/MM/YYYY 14h32mins
$date = DateTime::createFromFormat('d/m/Y h\hi\m\i\n\s',$yourString);
if($date === false){//if format does not fit
exit('Your wrong !');
}
I have an application that uses time() to record the time a topic was posted.
Very bad practice. In MySQL you have DateTime format (and each relational data base system has his own equivalent) to store date times and there are fully compatible with the DateTime constructor in PHP so migrate to it (use FROM_TIMESTAMP to change into DateTime).

Categories