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! |
<a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. FREE! Go There Now!
|
|
|
|
You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. 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!
|
|
|
|
Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities. FREE! Go There Now!
|
|
|
|
This tutorial shows new users of IBM WebSphere Business Monitor Version 6.0.2 how to perform the "Hello World" equivalent for monitoring business process applications. It is intended to help you get familiar with the capabilities of the product. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
|
|
|
|
As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security. FREE! Go There Now!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |