Ever got confused when you are coding a PHP application that needs to output date and time in Different formats and Time Zones?
Using conventional methods makes you write complicated functions to deal with it. However, If you use UNIX Timestamps as the Time data (in the database) , you can convert that into any format with just a simple function (consisting just 4 lines of code!)
Another reason to use the timestamp method is for multiple DBMS compatibility, MySQL stores it in 2007-04-12 08:00:00 format, but others have their own way of storing Datetime values. So it is considered safe that you store timestamps as a Integer, which can be converted into any format easily.
Consider the following function
function get_date($timestamp = "", $timezone = "", $format = "", $mysql = 'no')
{
$timestamp = ($timestamp == "") ? time() : $timestamp;
$format = ($format == "") ? (($mysql == 'mysql') ? Y-m-d H:i:s" : "F j, Y g:i a") : $format;
$timezome = ($timezone == "") ? 0.0 : $timezone;
return gmdate($format, $timestamp + (3600 * $timezone));
}
Just using get_date(); returns the current date in “April 12, 2007 8:00 am” format and get_date('','5.5','','mysql'); returns the current date in MySQL Format adjusted to GMT +5.5 Timezone.
Default use: get_date(timestamp, timezone, format, 'mysql');
Where the ‘timezone’ is the Adjustment needed in Decimal Format(i.e. +5:30 is 5.5 and so on.) and ‘format’ is the same as PHP’s date parameters.
Pretty Neat Uh? And not to mention that the function is highly extensible, And definitely makes stuff easier to manage. I personally use that function in most of my applications.
(The function was derived from phpBB)
Responded on April 16th, 2007 at 12:05 am
Dude, How does one learn PHP?
Responded on May 10th, 2007 at 12:58 am
>
>>Dude, How does one learn PHP?
>
Ya! I also want to know it ?
Responded on May 10th, 2007 at 12:17 pm
Its one of the Easiest Web-programming languages, Easier than C!!
Just get a good book, I’d Suggest “Beginning PHP5, MySQL, Apache Web Development” by Wrox, It gets you started in the right path.
After that visit some sites like http://www.phpbuilder.com/ and http://www.phpclasses.org/ and Try out the Complex Scripts, etc.
Then couple AJAX with PHP! (I’m yet to perfect it. Can’t find the book I want anywhere, and I can’t concentrate with ebooks. Oh Well..)