How to substract number of days in PHP [duplicate] - php

This question already has answers here:
Subtract 1 day with PHP
(9 answers)
Closed 7 years ago.
I have to compute a specific date considering the number of days I want to substract.
My code looks roughly like this:
$datac = date("Y-m-d");
$data = strtotime("-1 day" ,$datac);
But the output is: -84384
I don't understand what I do wrong.
It should work something like date -1 and show the date from yesterday. Thank you!

$datac = new DateTime();
$datac->modify('-1 day');
$data = $datac->format('Y-m-d');

$datac = date("Y-m-d");
$data = date("Y-m-d", strtotime('-1 day' . $datac));

Related

How to add one day to a date which is given as 'month-day'? [duplicate]

This question already has answers here:
Adding one day to a date
(13 answers)
Closed 5 years ago.
I have a date which is:
$firstDay = "'" . date('m-d', easter_date(date('Y'))) . "'";
Output of $firstDay is: '04-16'. Now I need to create a second variable and add 1 day to the $firstDay variable. How should I do that? I red that I can do with $date->modify(), I'm trying like so:
$secondDay = $firstEasterDay->modify('+1 day');
But this doesn't work and I got an error.
Thanks for any help.
<?php
$date = "04-15-2013";
$date = strtotime($date);
$date = strtotime("+1 day", $date);
echo date('m-d-Y', $date);?>
Try This

How to set a date of n number of days ago in php [duplicate]

This question already has answers here:
Subtracting days, months or years from date using php [closed]
(4 answers)
Closed 5 years ago.
I have a php variable:
$datevar = date("Y-m-d");
Which shows me the CURRENT date in the specified format.
What I want is the date from 7 days ago for CURRENT date in that format.
I have tried:
$datevar = $datevar - 7;
and
$datevar = date("Y-m-d") - 7 ;
But they both cause an error.
I refer to this solution: https://stackoverflow.com/a/3727821/7454754
So in your example this would be something like
<?php
$today = date("Y-m-d");
$newDate = date("Y-m-d", strtotime($today . " - 7 days"));
?>
Try the code below, that should work:
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " -1 week");
http://php.net/manual/en/function.strtotime.php
You can do it like this. Here we are using DateTime and DateInterval
Try this code snippet here
<?php
$datevar = date("Y-m-d");//your date
$dateTimeObj=new DateTime($datevar);
$dateTimeObj->sub(new DateInterval("P7D"));//subtracting 7 days
echo $dateTimeObj->format("Y-m-d");

Php have function for plus date or not? [duplicate]

This question already has answers here:
Adding one day to a date
(13 answers)
Closed 6 years ago.
Php have function for plus date or not ?
normally, if i want to plus date with 5 day i usually user this code.
<?PHP
$now = date("Y-m-d");
$now_explode = explode("-", $now);
$now_year = $now_explode[0];
$now_month = $now_explode[1];
$now_date = $now_explode[2];
$now_date = $now_date + 5;
$next_five_date = $now_year."-".$now_month."-".$now_date;
echo $next_five_date;
?>
But i have some issue eg: if $now = "2016-12-31";. When i run my code. Result will be 2016-12-36 Then i have to check month, check year for date in Feb month.
It's labyrinthine, so i want to know php have general function for plus date in to date ?
Here's one of many ways to do it:
$now = date("Y-m-d"); //how to get current date
$next_five_date = date('Y-m-d', strtotime('+5 days')); //how to get date +5 days

How To Add +6 Months in current date [duplicate]

This question already has answers here:
Add six months in php
(5 answers)
Closed 6 years ago.
i want to add 6 months in current date. i have used the following date format.
$current_time = date('Y-m-d');
i have proceed with this method shown below but an error is occured.
'A non well formed numeric value encountered'.
and print this value
[expiring_plan_date] => 1970-01-02
$data['PlanPayment']['expiring_plan_date'] = date
(
"Y-m-d",
strtotime("+6 month", $current_time)
);
please help..
Remove comma from strtotime
$data['PlanPayment']['expiring_plan_date'] = date("Y-m-d", strtotime("+6 month $current_time));

Get first day of week in "Y-m-d" of given week number in the year [duplicate]

This question already has answers here:
Get first day of week in PHP?
(39 answers)
Closed 10 years ago.
im trying to get the first day of a given week based on inputting a week number and year. im currently using this for the current week and year and works like a charm.
$week = date('W');
$year = date('Y');
$date1 = date('Y-m-d', strtotime($year."W".$week.'1'));
however when i try to input my own week and year it doesn't work
$week = 7;
$year = 2013;
$date1 = date('Y-m-d', strtotime($year."W".$week.'1'));
what type if string should i be using for strtotime? how come providing my own date doesn't work?
Because the week numbers are supposed to be padded (and the extra 1 at the end doesn't really do anything):
$date1 = date(
'Y-m-d',
strtotime($year . 'W' . str_pad($week, 2, '0', STR_PAD_LEFT))
);

Categories