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

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

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

How to substract number of days in PHP [duplicate]

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

find the last day/date of a WEEKOFYEAR with php [duplicate]

This question already has answers here:
How to get the first day of a given week number in PHP (multi-platform)?
(9 answers)
Closed 9 years ago.
I have a value that is the number for the weekofyear (between 1-52 ). I want to find out the date (yyyy-mm-dd) for the last day of that week.
I would prefer to do it in PHP rathe then MYSQL.
This can be easily solved with DateTime::setISODate() method:
$week = 50;
$dt = new DateTime();
$dt->setISODate($dt->format('o'), $week, 7);
echo $dt->format('Y-m-d');
demo
Or you can just create DateTime object (or unix timestamp with strtotime()) with ISO-8601 format like 2014-W50-7:
$week = 50;
$iso = sprintf("2014-W%02d-7", $week);
$dt = new DateTime($iso);
echo $dt->format('Y-m-d');
echo date('Y-m-d', strtotime($iso)); # or using strtotime()
demo

Generating week start and end dates with timezone offset [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert time and date from one time zone to another in PHP
I have two variables which show the weeks start and end date as follows:
$week = strtotime('-' . date('w') . ' days');
$start = date('Y-m-d', $week);
$end = date('Y-m-d', strtotime('+6 days', $week));
How can I offset these dates for a different timezone (e.g. PST?)
This will convert a tie in your servers timezone to the given timezone.
$datetime = new DateTime;
$newTZ = new DateTimeZone('Your/TimezoneHere');
$datetime->setTimezone($newTZ);

Categories