Adding time to a date php [duplicate] - php

This question already has answers here:
Add number of days to a date
(20 answers)
Closed 8 years ago.
here is my current issue:
I need to add a predefined amount to a selected date. I have been using this until now:
$date=date('Y-m-d', strtotime('+7 days'));
but this returns the current date +7 days.
How can I define the current date and the modify that using this?
lets say that I have the date defined as:
$udate='2014-05-06';
I need to add 2 months to this date.

You can do,
date('Y-m-d',strtotime(date("Y-m-d", strtotime($your_date)) . " +2 months"));
You can also do using DateTime object,
$date = new DateTime($your_date);
$interval = new DateInterval('P2M');
$date->add($interval);
echo $date->format('Y-m-d')

you can use:
$date = "2014-08-25";
$newdate = strtotime ( '+2 months' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-d' , $newdate );

Related

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

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

How to get last day of the month using php [duplicate]

This question already has answers here:
How to find the last day of the month from date?
(30 answers)
Closed 9 years ago.
How to get last day of the month using php ?
<?php
echo date('?-?-?');
?>
<?php
$day=new DateTime('last day of this month');
echo $day->format('M jS');
?>
The date function documentation says that t represents number of days in current month:
$day = date( 't-m-Y' );
try this
$day=date('Y-m-t'); // gives last day of current month
OR
$d = new DateTime( '2013-05-03' );
echo $d->format( 'Y-m-t' );
Try:
$last_day = date('t-m-Y');
where t means the last date of the current month.
PHP: date - Manual
How can I find the first and last date in a month using PHP?
Using the function date you would do
$day = date("t");
Please read the documentation
Try this
$date = new DateTime();
$lastDayOfMonth = $date->modify(
sprintf('+%d days', $date->format('t') - $date->format('j'))
);

php get oldest record and add 1 week [duplicate]

This question already has answers here:
php date format YYYY-MM-DD minus or add one week from now?
(4 answers)
Closed 9 years ago.
I'm looking at making an automated post once a week blog so I can create more postings and then have them go out once a week. I'm having some trouble getting the greatest time and adding 1 week to it.
My database I'm using "datetime".
So the string is in "2013-03-20 09:42:41".
I can get the value of greatest post blog_date, but how do I add 1 week to the string?
date('$blog_date', strtotime("+1 week"));
Thanks for your time ^^
ANSWER WORKS:
$blog_date = date('Y-m-d h:i:s', strtotime("+1 week", strtotime($newest)));
You can use the Datetime object to add a week easily
http://php.net/manual/en/book.datetime.php
$date = new DateTime('2013-03-20 09:42:41');
$date->modify('+1 week');
Try this..
$blog_date = "2013-03-20 09:42:41";
$date2 = strtotime(date("Y-m-d", strtotime($blog_date)) . "+1 week");
echo date('Y-m-d', $date2);
Output
2013-03-27

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