Subtracting a Certain Number of Hours, Days, Months or Years from Date

Subtracting a certain number of hours, days, months or years from date

For hours:

function get_offset_hours($hours)
{
return date('Y-m-d H:i:s', time() + 3600 * $hours);
}

Something like that will work well for hours and days (use 86400 for days), but for months and year it's a bit trickier...

Also you can also do it this way:

$date = strtotime(date('Y-m-d H:i:s') . ' +1 day');
$date = strtotime(date('Y-m-d H:i:s') . ' +1 week');
$date = strtotime(date('Y-m-d H:i:s') . ' +2 weeks');
$date = strtotime(date('Y-m-d H:i:s') . ' +1 month');
$date = strtotime(date('Y-m-d H:i:s') . ' +30 days');
$date = strtotime(date('Y-m-d H:i:s') . ' +1 year');

echo(date('Y-m-d H:i:s', $date));

Subtract days, months, years from a date in JavaScript

You are simply reducing the values from a number. So substracting 6 from 3 (date) will return -3 only.

You need to individually add/remove unit of time in date object

var date = new Date();
date.setDate( date.getDate() - 6 );
date.setFullYear( date.getFullYear() - 1 );
$("#searchDateFrom").val((date.getMonth() ) + '/' + (date.getDate()) + '/' + (date.getFullYear()));

Subtracting days, months or years from date using php

use strtotime('date -years -months -days')

  <?php
$time = strtotime('2001-11-14 -3 years -7 months -5 days');
echo $date = date("Y-m-d", $time);

Finding date by subtracting X number of days from a particular date in Javascript

Simply:

yourDate.setDate(yourDate.getDate() - daysToSubtract);

Is it possible to subtract days, months or years from current date in swift

You should use Calendar to do these calculations instead of hard coding 86400 for a day.

if let date = Calendar.current.date(byAdding: .day, value: -7, to: Date()) {
// Use this date
}

How to subtract months from a date stored in variable?

You can also use modify function.

$date = new DateTime('2014-04-01');
$date->modify('-1 month');
echo $date->format('m-Y');

Why does adding and subtracting 2 months to a date not give back the same date?

TL;DR

A month is not a fixed duration. Adding or taking a month does not give the same "time shift" depending on which day you are.

The usual algorithm

to add or take months is the following :

  • try to land on the same day number (4th, 30th, 31st) as you started, just by changing the month
  • if you would land on an impossible date (like 31th September, 30th February, 29th February for some years) then just go the maximum allowed day number of this month

This implies that adding some months then taking out the same number of months will not necessarily give you back the same date.

Examples :

31st of some month + 1 month --> One would want to get to the 31th of next month

But if there is no 31st of next month (like for 31th of August, no 31st of September), then what to do ?

Usual interpretation would say that you want to go to the end of the month, this is 30th September (for rent or other monthly subscription, for instance)


But usually, 30th of some month - 1 month --> One would want to get to the 30th of the previous month.

That would lead to .... 30th of August. Not 31th of August.

Hence: some date + 1 month - 1 month does not necessarily give the original date !


Another example :

Start at the 30th of August.

Take a month -> 30th of July

Add a month -> You want to get to 30th of August (same number, next month) or to the end of August ?

The default algorithm will try to give the same day number -> 30th of August (which is more logical now)


Also with days...

Note that the same problem happens with days,but much less often ! When some days don't have the same number of hours, for daylight saving days, when adding and taking same number of days you might not get back to the original date and time as you started from.

How to subtract/add days from/to a date?

Just subtract a number:

> as.Date("2009-10-01")
[1] "2009-10-01"
> as.Date("2009-10-01")-5
[1] "2009-09-26"

Since the Date class only has days, you can just do basic arithmetic on it.

If you want to use POSIXlt for some reason, then you can use it's slots:

> a <- as.POSIXlt("2009-10-04")
> names(unclass(as.POSIXlt("2009-10-04")))
[1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst"
> a$mday <- a$mday - 6
> a
[1] "2009-09-28 EDT"


Related Topics



Leave a reply



Submit