473,804 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
}

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

?>
Nov 5 '05 #2

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

Similar topics

6
7371
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 = "SELECT * FROM basics WHERE creation_date = DATE_ADD(creation_date, INTERVAL -30 DAY)"; ....doesn't do anything... Thanks for any help.
20
2642
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 functions like DATE() or NOW(), I don't get a Unix value, (but rather a formatted date). So I cannot use those functions to check against my saved value.
1
5638
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. If I use NOW() it works, but using DATE_ADD it doesn't work anymore. I want the datetime of the mysql server, not the apache server.
3
2662
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 MINUTES)' WHERE _userid = '$_userid' LIMIT 1"; and when run I see NO ERRORS. However, either the field in the DB doesn't change OR this expression is generating a value of '0' for the _sessexp
1
1639
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 not in USA - MySQL CONVERT_TZ's function doesn't work So, how can I do the following: 1. Today's visits $query = "SELECT count(*) FROM visits WHERE now() = date";
8
1936
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 digits. dbConnect('db');
6
38949
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 Unix-style timestamp and then formats it. $timestamp = "2005-04-06 15:43:34"; $time = strtotime($timestamp); print date('Y-m-d \a\t H:i', $time)."\n"; However, it seems kind of counter productive. After all, aren't people
0
3756
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 NULL for the second field. Here's my query: SELECT
5
1827
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 with couple of queries.
0
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.