UK Human Readable to Epoch Timestamp

Coyx 1 Tallied Votes 244 Views Share

Hi all,

First tutorial submission.

The standardised strtotime() function really grinds my gears. I hate messing about with dates in different formats... so I mullered this little function up. It works perfectly - probably easier ways to do it, I don't know... I just include this function in my functions files and whenever I want to change a UK based human readable date to a timestamp... I run it through this; quick and simple! Hope you find it useful!

function UKtoEpoch($date){
	$date = explode("/",$date);
	$date = $date[1] . "/" . $date[0] . "/" . $date[2];
	$date = strtotime($date);
	return $date;
}

$humandate = "31/12/2013"; //31st December 2013
echo UKtoEpoch($humandate);
Member Avatar for diafol
diafol

Have you looked at the DateTime class? That will take all manner of inputs and give you whatever you want. It's especially useful as it can add and substract time too. For example:

$date = DateTime::createFromFormat('j/m/Y', $date); //e.g. '21/10/2009'
echo $date->format('Y-m-d'); //output 2009-10-21
echo $date->format('U'); // output unix time

Or with your example, just change the '/' to '-':

$cDate = str_replace('/','-',$date);
$time = strtotime($cDate);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.