Date Time Code

  Home arrow Date Time Code arrow Current Age Script (v2)
DATE TIME CODE

Current Age Script (v2)
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2005-07-02

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    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

    blog comments powered by Disqus

    DATE TIME CODE ARTICLES

    - DaysInSpan.php
    - MySQLdateSpan.php
    - DateSpan.php
    - FutureDateFormEntry.php
    - Generate Time Option List for Select stateme...
    - Current Age Script (v2)
    - class for some mysql imitated time functions
    - Current Age Script, up to the last day
    - filemtime_remote
    - Page Generation Time Figure-Outer
    - convGMT v2
    - Benchmarker
    - Simple PHP Calendar
    - Display message according to hour of day
    - Display Date Function

    Developer Shed Affiliates

     



    © 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap