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! |
David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
|
|
|
|
Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base. FREE! Go There Now!
|
|
|
|
Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
|
|
|
|
Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications. 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!
|
|
|
|
Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
|
|
|
|
Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads. FREE! Go There Now!
|
|
|
|
You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online. FREE! Go There Now!
|
|
|
|
The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |