Subtract 1 Day with PHP

Subtract 1 day with PHP

You can try:

print('Next Date ' . date('Y-m-d', strtotime('-1 day', strtotime($date_raw))));

How to subtract 1 day 16 hours from datetime in PHP

Use date_sub/DateTime::sub.

$date = date_create("2016-02-29 08:30 PM") ;
date_sub($date, date_interval_create_from_date_string('1 day 16 hours'));
echo date_format($date, 'Y-m-d h:i a'); // 2016-02-28 04:30 am

If you want add an amount of time instead of subtracts it, use date_add instead.

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

Subtract 1 day from first day of the year not working as expected

You are not actually subtracting a day or a year from the beginning of the year, you are subtracting them from the current date. Also, because you are outputting using Y-01-01 format, both dates will always have 1 for the month and day. You need to first generate the first day of the year and then subtract from that and output using Y-m-d format:

$foy = strtotime(date('Y-01-01'));
var_dump(date('Y-m-d', strtotime('-1 day', $foy)));
var_dump(date('Y-m-d', strtotime('-1 year', $foy)));

Output:

string(10) "2018-12-31" 
string(10) "2018-01-01"

Demo on 3v4l.org

Subtract days from date

You can't use date to substract or add days (hours, minutes etc.), because, as per PHP manual, date:

Returns a string formatted according to the given format

And, well, you can't do what you want to a string.

To achieve what you want going the object oriented style, you have to create a DateTime object first:

$date = new DateTime(); // this will return today OR
$date = new DateTime('2010-05-20'); // this will return the needed date

Next, you may use sub to subtract or add to add a date interval:

$date->sub(new DateInterval('P10D'));
echo $date->format('Y-m-d');

This will output 2016-02-04

Update

You requested your code example:

$date = new DateTime('01/28/2016');
$date->sub(new DateInterval('P14D'));
echo $date->format('m/d/Y');

this will output

01/14/2016

Of course, don't forget to substitute 01/28/2016 for $statement_date

PHP subtract 1 month from date formatted with date ('m-Y')

 <?php 
echo $newdate = date("m-Y", strtotime("-1 months"));

output

07-2016

subtract 2 days from a date which is in m-d-Y H:i:s format in PHP

$currentdate= date('m-d-Y H:i:s');
echo $currentdate; //gives 06-22-2019 14:58:55

$date = date('m-d-Y H:i:s ', strtotime('-2 days', strtotime(date('Y-m-d H:i:s'))));
echo $date; //gives 06-20-2019 14:58:55

Building a function using Carbon to subtract 1 working day

I have managed to sort it using parse

$this=Carbon::parse($start)->subWeekdays(1)->toDateString();

Add 1 year to Date and minus 1 Day at the same time

DateTime() accounts for leap years:

// PHP 5.2+
$dt = new DateTime($rows['CoverStartDate']);
$dt->modify('+1 year');
$dt->modify('-1 day');
$renewalDate = $dt->format('d F Y');

or:

// PHP 5.3+
$dt = new DateTime($rows['CoverStartDate']);
$dt->add(new DateInterval('P1Y'));
$dt->sub(new DateInterval('P1D'));
$renewalDate = $dt->format('d F Y');

or:

// PHP 5.5+
$renewalDate = (new DateTime('04/09/2013'))->add(new DateInterval('P1Y'))
->sub(new DateInterval('P1D'))
->format('d F Y');


Related Topics



Leave a reply



Submit