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.
Related
This question already has answers here:
Get the date of next monday, tuesday, etc
(10 answers)
Closed 11 months ago.
How we can get previous week monday date and next week monday date by provided date.Eample: if $date = '2015-04-08' (y-m-d format).
then function returns, previous monday date = 2015-03-30
and next monday date = 2015-04-13
echo "Next Monday:". date('Y-m-d', strtotime('next monday', strtotime($givenDate)));
echo "Previous Monday:". date('Y-m-d', strtotime('previous monday', strtotime($givenDate)));
you can use
date('Y-m-d', strtotime('last week Monday'));
date('Y-m-d', strtotime('next week Monday'));
or whatever you want http://php.net/manual/en/datetime.formats.relative.php
You can use this:
$nextMonday = new \DateTime('2015-04-08 next Monday');
$previousMonday = new \DateTime('2015-04-08 Monday ago');
This question already has answers here:
How do I return the previous Sunday from 7 days ago using PHP date()?
(4 answers)
Closed 9 years ago.
I want to create a "pic of the week" on my website and have it dynamically change from Sunday - Sunday. So once the next Sunday comes around, it changes to that day. Here's what I got so far:
function getLastSunday(){
if (1==1)
{
$today = date('F j Y');
$lastSunday = date('m/d/Y', strtotime('last Sunday', $today));
}
return $lastSunday;
}
It is returning "01/01/1970". I want it to return "10/13/2013" and next week I want it to return "10/20/2013"
You are passing a string as the second argument to strtotime(), it really wants a number of seconds according to the PHP documentation.
Making that a time (or maybe even just ignoring it?) should make it work better:
function getLastSunday(){
if (1==1)
{
$today = date('F j Y');
$lastSunday = date('m/d/Y', strtotime('last Sunday', strtotime($today)));
}
return $lastSunday;
}
What you really want is this though I think:
function getLastSunday() {
return date('m/d/Y', strtotime('last Sunday'));
}
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
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get first day of week in PHP?
Given a timestamp I need to find the first day the week the timestamp belongs to.
e.g.
$format = "first day of week";
//convert to time
$time = strtotime("2011-07-01 00:00:00");
//format the time to the first day of the week
$date = strtotime($format,$time);
//Put the first day of the week into a pretty format
$weekStartDate = date("Y-m-d",$date);
The week start should equal 2011-06-27 at the moment its gone horribly wrong and equals 1970-01-01 which suggests to me the “first day of week” format is invalid.
Any ideas would be much appreciated thanks,
$time = strtotime("2011-07-01 00:00:00");
$weekStartDate = date('Y-m-d',strtotime("last Monday", $time));
Test it :
$time=[Your Request Time];
$dayofweek = date("w",strtotime($time));
if( $dayofweek != 0 )
$firstdayOfWeek = strtotime( $time . " - " . $dayofweek . " days " );
else
$firstdayOfWeek = strtotime($time);
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'));