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! |
Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance. FREE! Go There Now!
|
|
|
|
Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
|
|
|
|
Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download the latest trial version of IBM Data Studio V1.1 at no cost. IBM Data Studio is a comprehensive data management solution that helps you effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life cycle utilizing a consistent and integrated user interface. Unlike other client-side data management solutions that focus on only one aspect of the application lifecycle or database administration, Data Studio complements the Rational Software Delivery platform, providing unparalleled flexibility for a heterogeneous data server environment across platforms. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration. FREE! Go There Now!
|
|
|
|
As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
|
|
|
|
Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. 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!
|
|
|
|
Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |