Avatar of drcyrus3d
drcyrus3d

asked on 

Increase/decrease a given date.

Hello,

I would like to increase or decrease a certain date, not the current date, but a date passed as a string.

This works fine but only with the current date:
echo "Next day : ". date('Y-m-d', strtotime('+1 days', $time));

I want to pass my date to get the day (+/- x days) date.

Can you help me out with this?

Thanks.
PHP

Avatar of undefined
Last Comment
drcyrus3d
Avatar of elfe69
elfe69
Flag of Switzerland image

$myTimestamp = strtotime($myDate);
$myTimestamp += 86400; // 86400 = 60 seconds * 60 minutes * 24 hours = 1 day
echo "Next day : ". date('Y-m-d', $myTimestamp);
$inc=7;
$newdate = date('Y-m-d', strtotime($inc.' days'));
Well, with your particular code it would be

$inc=-7;  //plus or minus increment
echo "Next day : ". date('Y-m-d', strtotime($inc.' days', $time));
Avatar of geoffreyreemer
geoffreyreemer

Yeah, or this:

<?php
$add_days = 10      // Let's add 10 days!
$realtime = mktime(date("H"), date("i"), date("s"), date("n"), date("d") + $add_days, date("Y"));
$date = date("d-m-Y H:i:s", $realtime);
echo $date;
?>

If you want to substract days, just use a negative value for $add_days.

With this example, you don't need to convert everything to seconds.
Avatar of drcyrus3d
drcyrus3d

ASKER

Guys you didn't get the ideea. I want to use my date ... something like this;

<?
$current_date = "2006-12-28";
echo "Next day : ". date($current_date, strtotime('+1 days', $current_date));
?>

I don't want to use the current date, that's working fine, i have a problem with a resulted date from wich i have to iterate.

Thank you.
Ah right. In that case, the second answer of yodercm was the right one, with $time being the date you want to use.
ASKER CERTIFIED SOLUTION
Avatar of elfe69
elfe69
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Elfe: no, that's not the right answer. Cause if you wanna add or substract more than 1 days, you'll have to multiply it by 86400 all the time. Yoder's answer is far better, but hey... nice try, mate :-).
Avatar of drcyrus3d
drcyrus3d

ASKER

Thank you all for your effort.

Thank you elfe69, perfect ... exactly what i needed.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo