473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

?>
Nov 4 '05 #1
1 7179
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);
}

?>
Nov 5 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ralph Freshour | last post by:
I have a Date type in my MySQL table - I'm trying to do a query on all rows within the last 30 days based on that Date field - I'm having trouble figuring out how to form the query??? $php_SQL =...
20
by: Sims | last post by:
Hi, I have a field in my DB that saves the date/time as a integer. I get the time on my server using the time() function. now, moving away from php and looking at (My)SQL only. If I use...
1
by: Bob Bedford | last post by:
Hi, I'm trying this query: insert into logpay(datetime,ip,typepay,idannounce) values(DATE_ADD(NOW(),INTERVAL 14 DAYS),'255.255.255.255',2,178) I want to add 14 days from now in datetime field. ...
3
by: bobmct | last post by:
In my feeble attempt to keep track of login session timeouts I have the following code in my login section of my program: $sql = "UPDATE subscriber SET _sessexp = 'DATE_ADD(NOW(),INTERVAL 15...
1
by: rija | last post by:
Hi folks, I need help regarding date and time comparison in PHP and MySQL My website is hosted in the USA (GMT - 8) And It is managed in Madagascar (GMT + 3) Want to consider DATE in Madagascar...
8
mmarif4u
by: mmarif4u | last post by:
Hi everybody... I want to enter values to db like the following,,, Format is like this (810605-14-6356) This is the rite format, No a to z letters... 6 digits then - then 2 digits then - then 4...
6
by: Brandon | last post by:
I'm using PHP with MySQL 4.x and was having trouble converting a datetime from MySQL into a formatted string until I ran across this solution that converts a YYYY-MM-DD HH:MM:SS string into a...
0
by: Peter Redding | last post by:
Hi, I have a query that uses a user variable to get the latest date from a table using the MAX function. I then need to add 1 month onto this date using DATE_ADD but the query just returns a...
5
by: M. Savas Zorlu | last post by:
Hi, I just decided to leave Mssql and start using Mysql. I am having some problems with my queries; is there anyone here who has a knowledge of query syntax in Mysql? If so, I need some help...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.