Solved

php subtract days from string date

Posted on 2009-04-01
7
2,059 Views
Last Modified: 2013-12-12
i have a string, in european date format like this: 24/11/2009
i would like to subtract 28 days from the date and as output get a string variable with value 27/10/2009 (output in this format exactly)

i tried smth like the code below, but it does not work, could you please help me get the code right?
date_default_timezone_set('Europe/London');
$mudate = new DateTime("24/11/2009");
date_sub($mudate, new DateInterval("P28D"));

Open in new window

0
Comment
Question by:KristjanLaane
  • 3
  • 2
  • 2
7 Comments
 
LVL 9

Expert Comment

by:Rurne
ID: 24041068
Several things.

1.  Line 2 can fail, depending on your host.  DateTime expects a string as strtotime() can parse; according to the documentation, that's US English-style, so your switching of days and months might cause some problems.
2.  DateTime::sub doesn't actually modify the original DateTime object, but instead returns a new copy:
<?php
$mudate = new DateTime('2009-11-24', 'Europe/London');
$muinterval = new DateInterval('P28D');
$mudate->sub($muinterval);
echo $mudate->format('d/m/Y')."\n";    // prints '24/11/2009';
$newmudate = $mudate->sub($muinterval);
echo $newmudate->format('d/m/Y')."\n"; // prints '27/11/2009';
?>

Open in new window

0
 
LVL 5

Assisted Solution

by:pghn
pghn earned 100 total points
ID: 24041179
Try something like this:

(Since strtotime() I've found to be very good and reliable on parsing any date formate and coming up with the proper timestamp.)
$mudate = new DateTime("24/11/2009");
$newtimestamp = strtotime("-28 days",strtotime($mudate));
$newtimestamp = date("Y-m-d");

Open in new window

0
 
LVL 5

Expert Comment

by:pghn
ID: 24041192
Just a correction since you want in the format d/m/Y;

Line 3 it should be:
$newtimestamp = date("d/m/Y");

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:KristjanLaane
ID: 24061483
the second line of Rurne code and the first line of pghn code causes an error, so the subsequent lines are of no use at the moment. i do not seem to be able to declare a date, do you have ideas how i could get around that?

and not even the following works:


$mudate = date_create('2008-08-03 14:52:10');

Open in new window

0
 

Author Comment

by:KristjanLaane
ID: 24061513
might it be because my server is PHP Version 5.0.3 ? I cannot update the version at the moment as im not admin ...
0
 
LVL 9

Accepted Solution

by:
Rurne earned 400 total points
ID: 24061947
Yes.  DateInterval is not available for versions before 5.3.0.  Your alternative is to work something around strtotime(), get the timestamp, and manipulate it directly.  The below code is a fairly verbose example:
// setting up constants for explanation purposes only
define("SECONDS_PER_MINUTE", 60);
define("MINUTES_PER_HOUR", 60);
define("HOURS_PER_DAY", 24);
 
$day_interval = 28;  // aka DateInterval('P28D');
 
$beginning_timestamp = strtotime('2009-11-24');
$interval = $day_interval * SECONDS_PER_MINUTE * MINUTES_PER_HOUR * HOURS_PER_DAY;
$new_timestamp = $beginning_timestamp - $interval;
echo date('m/d/Y', $new_timestamp)."\n";  // prints "10/27/2009"

Open in new window

0
 

Author Comment

by:KristjanLaane
ID: 24064669
thanks, this works great ! strtotime seems to have read the date in in US format, so i changed my initial requirement of european data input and converted it to US date format first.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to fix Datetime in MySQL? 4 48
Delete  php pages of a part of our site 8 31
JQuery Search Filter 2 29
Formating field inside mysql query 2 13
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question