You are here: Re: mysql date_add function in PHP « PHP Programming Language « IT news, forums, messages
Re: mysql date_add function in PHP

Posted by windandwaves on 11/06/05 00:23

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 DateMysqlToTimestamp($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 DateTimestampToMysql($ts) {
$v = date('Y-m-d', $ts);
return $v;
}

//creates timestamp from normal date formatted as 21-12-2005
function DateNormalToTimestamp($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 DateTimestampToNormal($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 = DateMysqlToTimestamp($md);
$newts = DateAdd($interval, $n, $md);
$newmd = DateTimestampToMysql($newts);
if($nd) {
return dateconvert($newmd, "");
}
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(DateNormalToTimestamp($nd1), DateNormalToTimestamp($nd2));
}

function MysqlDaysDiff($md1, $md2) {
return DaysDiff(DateMysqlToTimestamp($md1), DateMysqlToTimestamp($md2));
}

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

?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация