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
Related
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 );
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'))
);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php strtotime “last monday” if today is monday?
I want to get some information from the database from this current week.
So what do i need? The first monday (date) and the next sunday (date).
This code works fine, but:
$fm = date('Y-m-d', strtotime('last monday', time()));
$lz = date('Y-m-d', strtotime('next sunday', time()));
But when it IS monday this doesnt work? It gives me a total different date.
What can i do to prevent this?
Sorry didnt search good enough.
$eerstedag = date('Y-m-d', strtotime('last monday', time()));
if (date('N', time()) == 1) $eerstedag = date('Y-m-d');
else $eerstedag = date('Y-m-d', strtotime('last Monday'));
This works for me.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP last day of the month
Is there any function like $date->getMonthDays() or $date->getLastDayOfMonth() in PHP to get the number of days in a given month (or the last day number)?
$start = new DateTime('2012-02-01');
$end = clone $start;
// Interval = last day of the month minus current day in $start
$interval = $start->getLastDayOfMonth() - intval($start->format('j'));
$end->add(new DateInterval('P' . $interval . 'D'));
EDIT: thanks, voted to close, it's a duplicate, sorry for asking...
The php date function gives you the number of days in the month with 't'
date("t");
See: http://php.net/manual/en/function.date.php
It's simple to get last month date
echo date("Y-m-t", strtotime("-1 month") ) ;
echo date("Y-m-1", strtotime("-1 month") ) ;
at March 3 returns
2011-02-28
2011-02-1
t gives you the total number of days in the current month. j gives you the current day of the month.
Using modify and some subtraction from format-ing the datetime, you can get to the end of the month.
$date = new DateTime();
$lastDayOfMonth = $date->modify(
sprintf('+%d days', $date->format('t') - $date->format('j'))
);
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
PHP Day iterator
I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?
I want to create a loop which creates records for each day from today to one year later.
There is no input for dates.
From date which the form is submitted to the day 1 year after.
How can i do that ?
For example today when i submit the form today, i need a loop from 17/01/2011 to 17/01/2012
This should work
$next_year = strtotime('+1 year');
$current_time = time();
while($current_time < $next_year){
$current_time = strtotime('+1 day', $current_time);
print date('d-m-Y', $current_time);
}
You can then use the $current_time variable with date() to format the time in whatever way you wish.
<?php
echo date('Y-m-d', strtotime('+1 year'));