mysql date_add function in PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • windandwaves

    #1

    mysql date_add function in PHP

    Hi Folk

    Some of you may be interested in a function that allows you to add/substract
    from a date in PHP, using a MySql Dateformat (e.g. 2005-10-31) (NOT TESTED):

    Any comments appreciated.

    <?php
    /*

    $ms = mysql date (e.g. 2005-03-24)
    $ts = php timestamp
    $n = number to add/substract

    */

    //provide a mysql date, add a date and returns as mysql date
    function Mysql_DateAdd ($interval, $n, $md) {
    $ts = DateMysqlToTime stamp($md);
    $newts = DateAdd($interv al, $n, $md);
    $newmd = DateTimestampTo Mysql($newts);
    return $newmd;
    }

    //allows to add to date using y,q,m,w,d,h,n or s as interval
    function DateAdd ($interval, $n, $ts) {
    $ds = getdate($ts);
    $h = $ds["hours"];
    $n = $ds["minutes"];
    $s = $ds["seconds"];
    $m = $ds["mon"];
    $d = $ds["mday"];
    $y = $ds["year"];
    switch ($interval) {
    case "y":
    $y += $n;
    break;
    case "q":
    $m +=($n * 3);
    break;
    case "m":
    $m += $n;
    break;
    case "w":
    $f +=($n * 7);
    break;
    case "d":
    $f += $n;
    break;
    case "h":
    $h += $n;
    break;
    case "n":
    $n += $n;
    break;
    case "s":
    $s += $n;
    break;
    }
    $ts = mktime($h ,$n, $s,$m ,$d, $y);
    return $ts;
    }

    //creates timestamp from date formatted as 2005-02-21
    function DateMysqlToTime stamp($md) {
    $v = mktime ( 0 , 0, 0 , substr($md, 5, 2) , substr($md, 8, 2) ,
    substr($md, 0, 4));
    return $v;
    }

    //creates 2005-12-21 from a timestamp
    function DateTimestampTo Mysql($ts) {
    $v = date('Y-m-d', $ts);
    return $v;
    }



    ?>


  • windandwaves

    #2
    Re: mysql date_add function in PHP

    windandwaves wrote:
    Here is the file with a few more functions added

    <?php
    /*

    $ms = mysql date (e.g. 2005-03-24)
    $ts = php timestamp
    $nd = normal date (24-03-2005)
    */



    //allows to add to date using y,q,m,w,d,h,n or s as interval
    function DateAdd ($interval, $n, $ts) {
    $ds = getdate($ts);
    $h = $ds["hours"];
    $n = $ds["minutes"];
    $s = $ds["seconds"];
    $m = $ds["mon"];
    $d = $ds["mday"];
    $y = $ds["year"];
    switch ($interval) {
    case "y":
    $y += $n;
    break;
    case "q":
    $m +=($n * 3);
    break;
    case "m":
    $m += $n;
    break;
    case "w":
    $f +=($n * 7);
    break;
    case "d":
    $f += $n;
    break;
    case "h":
    $h += $n;
    break;
    case "n":
    $n += $n;
    break;
    case "s":
    $s += $n;
    break;
    }
    $ts = mktime($h ,$n, $s,$m ,$d, $y);
    return $ts;
    }

    //creates timestamp from date formatted as 2005-02-21
    function DateMysqlToTime stamp($md) {
    $v = mktime ( 0 , 0, 0 , substr($md, 5, 2) , substr($md, 8, 2) ,
    substr($md, 0, 4));
    return $v;
    }


    //creates mysql date (2005-12-21) from a timestamp
    function DateTimestampTo Mysql($ts) {
    $v = date('Y-m-d', $ts);
    return $v;
    }

    //creates timestamp from normal date formatted as 21-12-2005
    function DateNormalToTim estamp($nd) {
    $v = mktime ( 0 , 0, 0 , substr($nd, 3, 2) , substr($nd, 0, 2) ,
    substr($nd, 6, 4));
    return $v;
    }

    //creates normal date (12-12-2005) from a timestamp
    function DateTimestampTo Normal($ts) {
    $v = date('d-m-Y', $ts);
    return $v;
    }


    //provide a mysql date or normal date, add a date value and returns as mysql
    date / normal date
    function Mysql_DateAdd ($interval, $n, $md = "", $nd = "") {
    if($nd) {
    $md = dateconvert("", $nd);
    }
    $ts = DateMysqlToTime stamp($md);
    $newts = DateAdd($interv al, $n, $md);
    $newmd = DateTimestampTo Mysql($newts);
    if($nd) {
    return dateconvert($ne wmd, "");
    }
    else {
    return $newmd;
    }
    }

    function dateconvert($md = "", $nd = "") {
    if ($nd){ //from normal to mysql
    list($day, $month, $year) = split('[/.-]', $date);
    $date = "$year-$month-$day";
    return $date;
    }
    elseif ($md){ //from mysql to normal
    list($year, $month, $day) = split('[-.]', $date);
    $date = "$day-$month-$year";
    return $date;
    }
    }


    function NormalDaysDiff( $nd1, $nd2) {
    return DaysDiff(DateNo rmalToTimestamp ($nd1), DateNormalToTim estamp($nd2));
    }

    function MysqlDaysDiff($ md1, $md2) {
    return DaysDiff(DateMy sqlToTimestamp( $md1), DateMysqlToTime stamp($md2));
    }

    function DaysDiff ($ts1, $ts2) {
    $difference = abs($ts1 - $ts2);
    return ($difference / 86400);
    }

    ?>


    Comment

    Working...