a sequel to my Current Age Script, now it supports up to the last minute. The new catch though, is it only supports times after the unix epoch, because the main function im using here is strtotime(). But there is a workaround to this though, you can avoid using strtotime by specifying the month, day, year, hour and minute in the variables.
This could well be used in forums, for tracking how long the user has been away, or how old the post is, and stuff like that. Hope you enjoy.
P.S.
The class.grammar.php is just a grammar library I wrote which is responsible for concatenating the letters 's' to the words 'month', 'day', etc.. if the number is greater than one. It should be available on the archive.
By : voldomazta
<?
// echo ts_date('June 23, 1988 06:55AM');
// my birthday :)
function ts_date($str_date,$str_append = 'old'){
require('class.grammar.php');
$g = new Grammar;
$st = strtotime($str_date);
/* Date Variables */
$dv = array('mi'=>'i', 'h'=>'H', 'm'=>'n', 'd'=>'j', 'y'=>'Y');
/* Days in Month */
for($x=1 ; $x<=12 ; $x++) $dim[$x] = date('t',mktime(0,0,0,$x,1,date('Y')));
/* Given Variables */
foreach(array_keys($dv) as $gvt) {
$tgvt = 'g'.$gvt;
$$tgvt = date($dv[$gvt],$st);
}
/* Server Variables */
foreach(array_keys($dv) as $svt) {
$tsvt = 'c'.$svt;
$$tsvt = date($dv[$svt]);
}
/* Differences */
$mi = $cmi - $gmi;
$h = $ch - $gh;
$d = $cd - $gd;
$m = $cm - $gm;
$y = $cy - $gy;
if($mi < 0) {
$mi = ($cmi + 60) - $gmi;
$h = ($ch - 1) - $gh;
}
if($h < 0) {
$h = ($ch + 24) - $gh;
$d = ($cd - 1) - $gd;
}
if($d < 0) {
$d = ($cd + $dim[$cm]) - $gd;
$m = ($cm - 1) - $gm;
}
if($m < 0) {
$m = ($cm + 12) - $gm;
$y = ($cy - 1) - $gy;
}
if($y < 0) {
die('Invalid Date');
}
/* Grammar Operations */
if($y > 0) {
$r .= $y.' '.$g->plural('year',$y);
}
if($m > 0) {
if($y > 0) {
$r .= (is_zero($d, $h, $mi) ? ' and ' : ', ');
}
$r .= $m.' '.$g->plural('month',$m);
}
if($d > 0) {
if(is_pos($y, $m)) {
$r .= (is_zero($h, $mi) ? ' and ' : ', ');
}
$r .= $d.' '.$g->plural('day',$d);
}
if($h > 0) {
if(is_pos($y, $m, $d)) {
$r .= ($mi == 0 ? ' and ' : ', ');
}
$r .= $h.' '.$g->plural('hour',$h);
}
if($mi > 0) {
if(is_pos($y, $m, $d, $h)) {
$r .= ' and ';
}
$r .= $mi.' '.$g->plural('minute',$mi);
}
return $r.' '.$str_append;
}
function is_zero() {
foreach(func_get_args() as $arg) {
if((int)$arg != 0) return false;
}
return true;
}
function is_pos() {
foreach(func_get_args() as $arg) {
if((int)$arg > 0) $pos += 1;
}
return ($pos > 0 ? true : false);
}
?>
Click to
Download File| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Date Time Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br /> FREE! Go There Now!
|
|
|
|
Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available. FREE! Go There Now!
|
|
|
|
The combination of Eclipse, DB2 Express-C 9.5, and WebSphere Application Server Community Edition 2.0 -- all free to download, use, and deploy -- is an excellent from-prototype-to-production suite for all of your Java and Java enterprise development needs. What might not be obvious is the relative ease with which you can use these proven tools to create, test, and deploy cutting-edge, lightweight applications as well. This tutorial guides you through the development of a small human-resources application, first using conventional JavaServer Pages (JSP) based technology, and then migrating it to a highly interactive solution using Ajax. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered! FREE! Go There Now!
|
|
|
|
Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
|
|
|
|
Here's a fun way to learn about DB2! Learn or teach the basics of DB2 and relational database with an interactive game called The DB2 Detective Game. The game teaches relational database concepts and shows how technology can be applied to solving real-life problems (the game's theme is a crime investigation). This tutorial has been updated for DB2 9. FREE! Go There Now!
|
|
|
|
Get a free trial download of IBM Lotus Forms V3.0 (formerly Workplace Forms), which provides a zero-footprint eForms solution to help you automate and move forms-based business processes off the desktop and onto the Web. With Lotus Forms, you can extend applications beyond the firewall by creating a single electronic form document ready for use in both thick and Web 2.0 thin client format. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
|
|
|
|
The Eclipse SOA Tools Platform (STP) plug-in and Apache Tuscany simplifies services development through the use of the popular Eclipse development environment. Apache Tuscany has also been integrated with the STP to provide a Service Component Architecture (SCA) Java run time for the services you create, allowing you to annotate your service using the SCA standard and Apache Tuscany annotations. In this tutorial, you will see STP and Apache Tuscany in action, through the creation of a Remote Method Invocation (RMI) service. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |