Zoklet.net

Go Back   Zoklet.net > Technology > Technophiles and Technophiliacs > Codes of all kinds

Reply
 
Thread Tools
  #1  
Old 08-28-2009, 11:50 AM
Axiom Axiom is offline
Duke
 
Join Date: May 2008
Thanks: 21
Thanked 53 Times in 42 Posts
Default PHP - A simple relative date function

Code:
<?php

 function convertToUnixTimestamp($dcDate) {

	# dcDate is in the format YYYYMMDDHHiiSS
	$unixTimestamp = mktime(substr($dcDate,8,2), substr($dcDate,10,2), substr($dcDate,12,2), substr($dcDate,4,2), substr($dcDate,6,2), substr($dcDate,0,4));
	return $unixTimestamp;

  }

  function relativeDate($targetDate) {

	# Convert date format from YYYYMMDDHHiiSS to Unix Timestamp
	$targetTimestamp = convertToUnixTimestamp($targetDate);
	$currentTimestamp = time();
	$difference = $currentTimestamp - $targetTimestamp;

	# Date is from the Future
	if ($difference < 0) {

		$theDate = 'In The Future';
		return $theDate;

	}

	switch ($difference) {

		# Greater Than 30 Days
		case ($difference > 2592000):
			$theDate = date('j M Y',mktime(substr($targetDate,8,2), substr($targetDate,10,2), substr($targetDate,12,2), substr($targetDate,4,2), substr($targetDate,6,2), substr($targetDate,0,4)));
			break;

		# Week(s) Ago (Condition represents greater than 6 days)
		case ($difference > 518400):
			$theDate = round($difference/518400) . ' week' . (round($difference/518400) > 1 ? 's' : '' ) . ' ago';
			break;

		# Day(s) or Hour(s) Ago (Condition represents greater than 59mins in seconds)
		case ($difference > 3540):

			# Generate Midnight Timestamp
			$midnightTimstamp = mktime(00, 01, 00, date("m"), date("d"), date("Y"));
			$differenceYesterday = $midnightTimstamp - $targetTimestamp;

			# Yesterday
			if ( (round($differenceYesterday) < 86400) && ($differenceYesterday > 0) ) {
			$theDate = 'yesterday ' . date('ga',mktime(substr($targetDate,8,2), substr($targetDate,10,2), substr($targetDate,12,2), substr($targetDate,4,2), substr($targetDate,6,2), substr($targetDate,0,4)));			

			# Day(s) Ago
			} else if ($difference > 86400) {
			$theDate = round($difference/86400) . ' day' . (round($difference/86400) > 1 ? 's' : '' ) . ' ago';

			# Hour(s) Ago
			} else {
			$theDate = round($difference/3600) . ' hour' . (round($difference/3600) > 1 ? 's' : '' ) . ' ago';
			}
			break;

		# Min(s) Ago
		default:
			$theDate = ceil($difference/60) . ' min' . (ceil($difference/60 > 1) ? 's' : '' ) . ' ago';
			break;

	}

	return $theDate;

  }

# 28 Aug 2009 20:30 and 40 secs
$targetDate = '20090828203040';
print relativeDate($targetDate);

?>
I store my dates in the database as an integer in the format YYYYMMDDHHiiSS (eg. 20090828190000). This means ordering complex queries by date becomes trivial but it also allows anyone to quickly view the date in the database without having to first convert it from a unix timestamp.

But what if you wanted to output the date in a human readable format like facebook or vbull (eg. 2 mins ago, yesterday 2pm)?

This relative date function:
  1. Takes a date field in the format YYYYMMDDHHiiSS.
  2. Converts it to a unix timestamp.
  3. Calculates how long ago the event occured relative to todays date and time.
  4. Returns the date in a human readable format.



.

Last edited by Axiom; 08-28-2009 at 11:56 AM.
Reply With Quote
The following users say "It is so good to hear it!":
Loophole (08-28-2009)
Reply

Bookmarks

Tags
date, function, php, relative, simple

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Solutions for Simple Minds bornkiller DIY 7 07-03-2009 12:57 AM
hitting your relative/friends by MISTAKE! 7i8_Dago Generally Speaking 10 07-01-2009 04:35 PM
Black Actor (morgan freeman) had affair with relative Major General Franklin Kirby The Trashcan 11 06-19-2009 07:42 AM
Would you use the ignore function? MartialShot Bat Country 13 02-03-2009 06:17 PM


All times are GMT. The time now is 12:00 AM.


Hot Topics
Join our Chatroom!
Users: 8
Messages/minute: 0
Topic: "Only rule: be nice or I'll cut your fucking face off, dumbshit"
Users: 27
Messages/minute: 1.6
Topic: "http://codelove.org :: Below is above in 2 codes 1 love. :: wh..."
Users: 18
Messages/minute: 5
Topic: "http://www.literotica...."
Advertisements
Your ad could go right HERE! Contact us!

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.