PHP DateConvert

Related Articles

Background

In the UK, we're all used to reading and writing the date as DD/MM/YYY - it's just the way things are done over here and it's the way we're all used to. In fact, I always thought that the American way (YYYY/MM/DD) was really stupid and I am sure that everyone around me still holds the same opinion. But it seems that the American way is better.

Why? U.S style dates that are sorted numerically still maintain their chronological order, whereas UK style dates don't - in the UK 20/07/2005 comes before 21/07/2001 (stupid, I know). As it's the better way, you'll find that wherever you find dates & timestamps (e.g. mySql, PHP etc.), you'll find them in the U.S style (i.e. YYYYMMDDHHMMSS).

The Function

To make use of these dates and timestamps, this shortcut uses a combination of the date() and mkdate() functions to convert them into something a bit more user-friendly for human guests.

<?php
function DateConvert($old_date, $layout) { 
	//Remove non-numeric characters that might exist (e.g. hyphens and colons)
	$date = ereg_replace('[^0-9]', '', $old_date);
	
	//Extract the different elements that make up the date and time
	$date_year = substr($old_date,0,4);
	$date_month = substr($old_date,5,2);
	$date_day = substr($old_date,8,2);
	$date_hour = substr($old_date,11,2);
	$date_minute = substr($old_date,14,2);
	$date_second = substr($old_date,17,2);
	
	//Combine the date function with mktime to produce a user-friendly date & time
	$new_date = date($layout, mktime($date_hour, $date_minute,
$date_second, $date_month, $date_day, $date_year)); return $new_date; } ?>

Examples

A full list of characters that you can use to customise the way in which the date can be formatted is available in the PHP Manual. Once you've had a look at that and got an understanding of the different elements, there are countless (well almost countless) ways in which you can format your dates:

<?php
$todays_date = "2005-07-01 12:35:01";

//We can use words to desccribe the date (as you see on on my blog)
print DateConvert($todays_date, "l, jS F Y"); //Friday, 1st July 2005

//We can use the UK styleprint 
DateConvert($todays_date, "d/m/Y"); //01/07/2005

//We can use the US styleprint 
DateConvert($todays_date, "Y/m/d"); //2005/07/01

//We can even add the timeprint 
DateConvert($todays_date, "Y/m/d - g:ia"); //2005/07/01 12:35pm
?>

And if you've a got a few styles in mind (e.g. UK & US) then you can predefine those layouts:

<?php
function DateConvert2($old_date, $style) { 
	//Remove non-numeric characters that might exist (e.g. hyphens and colons)
	$date = ereg_replace('[^0-9]', '', $old_date);
	
	//Extract the different elements that make up the date and time
	$date_year = substr($old_date,0,4);
	$date_month = substr($old_date,5,2);
	$date_day = substr($old_date,8,2);
	$date_hour = substr($old_date,11,2);
	$date_minute = substr($old_date,14,2);
	$date_second = substr($old_date,17,2);
	
	//Your pre-defined layouts
	if($style=="uk") 
	  $layout = "d/m/Y";
	elseif($style=="us") 
	  $layout = "Y/m/d";
	  
	//Combine the date function with mktime to produce a user-friendly date & time
	$new_date = date($layout, mktime($date_hour, $date_minute,
$date_second, $date_month, $date_day, $date_year)); return $new_date; } ?>

and call them by name:

<?php
$todays_date = "2005-07-01 12:35:01";

//We can use the UK styleprint 
DateConvert($todays_date, "uk"); //01/07/2005

//We can use the US styleprint 
DateConvert($todays_date, "us"); //2005/07/01 
?>

Comments

I beleive that in the US, dates are displayed using the MM/DD/YYYY format and what you refer to as "the American way" in this article is the International standard for displaying dates and times.

--
Mark, The UK Web Design Company


Publication Date: Sunday 3rd July, 2005
Author: Abu Aaminah View profile

Related Articles