I can get the date of the Monday for the current week, but how do I get it to display "and (the date for the monday of next week)"?
For example, "March 6 and 13, 2017"
This is what I have so far:
<?php echo date('F j, Y',strtotime(date('o-\WW')));?>
You can use the far more readable:
echo date('F j, Y',strtotime('Monday this week'));
echo date('F j, Y',strtotime('Monday next week'));
An exact script:
$this_monday = date("F j", strtotime("Monday this week"));
$next_monday = date("j, Y", strtotime("Monday next week"));
echo $this_monday . " and " . $next_monday;
The problem will be crossing months. If you need to do that, you'll need more logic, perhaps something like:
$this_monday = strtotime("Monday this week");
$next_monday = strtotime("Monday next week");
if (date("F",$this_monday) === date("F",$next_monday)) {
echo date("F j", $this_monday) . " and " . date("j, Y", $next_monday);
} else {
echo date("F j", $this_monday) . " and " . date("F j, Y", $next_monday);
}
Someone may come along and suggest using the Datetime class instead, and that might be something to put on your list to learn, as well.
Related
I need to get the first day of the week (Monday), 8 weeks back from today, where 8 is a variable.
What's the best way to do that in PHP?
You can do this way
echo date("l M-d-Y", strtotime('monday this week'));
echo date("l M-d-Y", strtotime('sunday this week'));
echo date("l M-d-Y", strtotime('monday last week'));
echo date("l M-d-Y", strtotime('sunday last week'));
echo date("l M-d-Y", strtotime('monday next week'));
echo date("l M-d-Y", strtotime('sunday next week'));
You can also search monthwise
echo date("l M-d-Y", strtotime('first day of this month'));
echo date("l M-d-Y", strtotime('last day of this month'));
echo date("l M-d-Y", strtotime('first day of last month'));
echo date("l M-d-Y", strtotime('last day of last month'));
echo date("l M-d-Y", strtotime('first day of next month'));
echo date("l M-d-Y", strtotime('last day of next month'));
$weeks = 8;
// Timestamp for $weeks weeks ago
$time = strtotime("$weeks weeks ago");
// Day of the week for $time (1 - Mon, ...)
$week_day = date('N', $time);
// Number of days from Monday
$diff = $week_day - 1;
// The date of the Monday $weeks weeks ago
echo date('j', $time - ($diff * 24 * 3600));
It's not really complicated actually, all you got to do is play a little bit with datetimes ;
<?php
$dt = new Datetime(sprintf('%d weeks ago', 8)); // replace 8 with variable, your value, whatever
$day = $dt->format('w');
$dt->modify(sprintf('%d days go', ($day - 1) % 7));
your $dt should then have the value you seek
You can make some math with dates in PHP like:
$now = date('F d, Y H:i');
$newdate = date('F d, Y H:i', strtotime($now.' - 8 weeks'));
echo $newdate;
In this case it will output a current date minus 8 weeks.
Also to count which day is today you can use:
$dw = date( "w", strtotime($newdate));
Where $dw will be 0 (for Sunday) through 6 (for Saturday) more informations can be found: PHP: date
Solution
In your case it would look as follows:
<?php
$weeks = 8;
$now = date('F d, Y H:i:s');
$newdate = date('F d, Y H:i:s', strtotime($now.' - '.$weeks.' weeks'));
$new_date_day = date( "w", strtotime($newdate));
$minus = $new_date_day - 1;
if ($minus < 0) { //check if sunday
$plus = $minus * -1;
$newdate = date('F d, Y H:i:s', strtotime($newdate.' + '.$plus.' days'));
} else {
$newdate = date('F d, Y H:i:s', strtotime($newdate.' - '.$minus.' days'));
}
echo $newdate;
?>
Of course you can echo what ever style of date you want. F.ex. F d, Y H:i:s will output November 28, 2016 06:18:03.
My server time is currently:
Sunday 15 December 11:18:00 EST 2013
Given the following script:
date_default_timezone_set("Australia/ACT");
$date1 = date('D, Y-m-d H:i:s', strtotime("+1 Weekday"));
$date2 = date('D, Y-m-d H:i:s', strtotime("+2 Weekdays"));
$date3 = date('D, Y-m-d H:i:s', strtotime("+3 Weekdays"));
$date4 = date('D, Y-m-d H:i:s', strtotime("+4 Weekdays"));
$date5 = date('D, Y-m-d H:i:s', strtotime("+5 Weekdays"));
$date6 = date('D, Y-m-d H:i:s', strtotime("+6 Weekdays"));
echo "+1 Weekday: " .$date1 . "<br>";
echo "+2 Weekday: " .$date2 . "<br>";
echo "+3 Weekday: " .$date3 . "<br>";
echo "+4 Weekday: " .$date4 . "<br>";
echo "+5 Weekday: " .$date5 . "<br>";
echo "+6 Weekday: " .$date6 . "<br>";
What would cause dates 5 to 6 return Sunday? Which isn't a weekday.
+5 Weekday: Sun, 2013-12-22 00:00:00
+6 Weekday: Mon, 2013-12-23 00:00:00
I'm expecting +5 Weekday to return Fri, 2013-12-20.
I'm running PHP 5.4.17-1 on Debian with default timezone set to Australia/Sydney.
This is a bug:
https://bugs.php.net/bug.php?id=63521
Just subtract 2 days when it returns a Sunday.
I have this PHP code:
$end=date('Y-m-d');
I use it to get the current date, and I need the date 5 years in the future, something like:
$end=date('(Y + 5)-m-d');
How can I do this?
Try with:
$end = date('Y-m-d', strtotime('+5 years'));
Modifying dates based on this post
strtotime() is really powerful and allows you to modify/transform dates easily with it’s relative expressions too:
Procedural
$dateString = '2011-05-01 09:22:34';
$t = strtotime($dateString);
$t2 = strtotime('-3 days', $t);
echo date('r', $t2) . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100
DateTime
$dateString = '2011-05-01 09:22:34';
$dt = new DateTime($dateString);
$dt->modify('-3 days');
echo $dt->format('r') . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100
The stuff you can throw at strtotime() is quite surprising and very human readable. Have a look at this example looking for Tuesday next week.
Procedural
$t = strtotime("Tuesday next week");
echo date('r', $t) . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100
DateTime
$dt = new DateTime("Tuesday next week");
echo $dt->format('r') . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100
Note that these examples above are being returned relative to the time now.
The full list of time formats that strtotime() and the DateTime constructor takes are listed on the PHP Supported Date and Time Formats page.
Another example, suitable for your case could be: based on this post
<?php
//How to get the day 3 days from now:
$today = date("j");
$thisMonth = date("n");
$thisYear = date("Y");
echo date("F j Y", mktime(0,0,0, $thisMonth, $today+3, $thisYear));
//1 week from now:
list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
echo date("F j Y", mktime(0,0,0, $thisMonth, $today+7, $thisYear));
//4 months from now:
list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
echo date("F j Y", mktime(0,0,0, $thisMonth+4, $today, $thisYear));
//3 years, 2 months and 35 days from now:
list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
echo date("F j Y", mktime(0,0,0, $thisMonth+2, $today+35, $thisYear+3));
?>
Use this code to add years or months or days or hours or minutes or seconds to a given date
echo date("Y-m-d H:i:s", strtotime("+1 years", strtotime('2014-05-22 10:35:10'))); //2015-05-22 10:35:10
echo date("Y-m-d H:i:s", strtotime("+1 months", strtotime('2014-05-22 10:35:10')));//2014-06-22 10:35:10
echo date("Y-m-d H:i:s", strtotime("+1 days", strtotime('2014-05-22 10:35:10')));//2014-05-23 10:35:10
echo date("Y-m-d H:i:s", strtotime("+1 hours", strtotime('2014-05-22 10:35:10')));//2014-05-22 11:35:10
echo date("Y-m-d H:i:s", strtotime("+1 minutes", strtotime('2014-05-22 10:35:10')));//2014-05-22 10:36:10
echo date("Y-m-d H:i:s", strtotime("+1 seconds", strtotime('2014-05-22 10:35:10')));//2014-05-22 10:35:11
You can also subtract replacing + to -
$date = strtotime($row['timestamp']);
$newdate = date('d-m-Y',strtotime("+1 year",$date));
Its very very easy with Carbon.
$date = "2016-02-16"; // Or Your date
$newDate = Carbon::createFromFormat('Y-m-d', $date)->addYear(1);
Using Carbon:
$dt = Carbon::now();
echo $dt->addYears(5);
To add one year to todays date use the following:
$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day"));
You may use DateInterval for this purpose;
$currentDate = new \DateTime(); //creates today timestamp
$currentDate->add(new \DateInterval('P5Y')); //this means 5 Years
and you can now format it;
$currentDate->format('Y-m-d');
Try below code, i hope it will be helpful for you
<?php
$current_date=strtotime(date('Y-m-d'));
echo $end = date('Y-m-d', strtotime('+5 years',$current_date));
?>
try this ,
$presentyear = '2013-08-16 12:00:00';
$nextyear = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($presentyear )), date("d",strtotime($presentyear )), date("Y",strtotime($presentyear ))+5));
echo $nextyear;
try this:
$yearnow= date("Y");
$yearnext=$yearnow+1;
echo date("Y")."-".$yearnext;
Try this code and add next Days, Months and Years
// current month: Aug 2018
$n = 2;
for ($i = 0; $i <= $n; $i++){
$d = strtotime("$i days");
$x = strtotime("$i month");
$y = strtotime("$i year");
echo "Dates : ".$dates = date('d M Y', "+$d days");
echo "<br>";
echo "Months : ".$months = date('M Y', "+$x months");
echo '<br>';
echo "Years : ".$years = date('Y', "+$y years");
echo '<br>';
}
I want to get the start and end date of the previous calender month.
So, it's July 2012 now, I want the function to return 01 June 2012 as start and 30 June 2012 as the end.
This is my code:
$current_month = date("Y-m-01 00:00:00");
$start_month = strtotime(date("Y-m-d", strtotime($current_month) . " -1 month"));
$end_month = strtotime(date("Y-m-d", strtotime($current_month) . " -1 second"));
echo "Start Month: " . date('Y-m-d',$start_month) . "( " . $start_month . ")<br>";
echo "End Month: " . date('Y-m-d',$end_month) . "( " . $end_month . ")<br>";
But it echo's:
Start Month: 2012-07-01( 1341093600)
End Month: 2012-07-01( 1341093600)
Any idea what I'm doing wrong?
Answering whats wrong with the code you posted, you just needed to move your round bracket over a little bit and you had it. :)
<?php
$current_month = date("Y-m-01 00:00:00");
$start_month = strtotime(date("Y-m-d", strtotime($current_month . " -1 month")));
$end_month = strtotime(date("Y-m-d", strtotime($current_month . " -1 day")));
echo "Start Month: " . date('Y-m-d',$start_month) . "( " . $start_month . ")<br>";
echo "End Month: " . date('Y-m-d',$end_month) . "( " . $end_month . ")<br>";
?>
Try this, (check the manual of strtotime)
$now = time();
$current_month = date("Y-m-01 00:00:00", $now);
$start_month = strtotime("-1 month", $now);
$end_month = strtotime("-1 second", $now);
Try something like this:
echo date("Y-m-d", mktime(0,0,0,date('m')-1,1,date('y')));
echo date("Y-m-d", mktime(0,0,0,date('m'),0,date('y')));
Seems a little bit oversized, what you (and the other answers :X) tried. It's justs
echo date('Y-m-d', strtotime('first day of last month'));
echo date('Y-m-d', strtotime('last day of last month'));
For arbitrary months
// 3 months in the past
echo date('Y-m-d', strtotime('first day of -3 months'));
echo date('Y-m-d', strtotime('last day of -3 months'));
// 3 months in the future
echo date('Y-m-d', strtotime('first day of +3 months'));
echo date('Y-m-d', strtotime('last day of +3 months'));
Read more At the manual
echo $firstdate= "01/".date('m')."/".date('Y') ;
$lastdateofmonth=date('t',date('m'));
echo $lastdate=$lastdateofmonth."/".date('m')."/".date('Y') ;
or
$date='2012-06-05 12:00:00';
echo "End = ".date("Y-m-t",strtotime($date));
echo "start = ".date("Y-m-01",strtotime($date));
Let's say I have this:
$a11 = date("F j, Y, g:i a", $a['date']);
$newTime = date($a['date'], strtotime('+3 hour'));
$b11 = date("F j, Y, g:i a", $newTime);
echo $a11 . " AND " . $b11;
I know $a['date'] is right because I get: March 22, 2011, 10:22 pm. However, the echo produces: March 22, 2011, 10:22 pm AND March 22, 2011, 10:22 pm when clearly the second part is suppose to be three hours ahead.
What am I doing wrong?
Don't you want:
$newTime = strtotime( '+3 hours',$a['date'] );
$b11 = date("F j, Y, g:i a", $newTime );
It seems you provide the wrong order of parameters in $newTime = date($a['date'], strtotime('+3 hour'));. Try this:
<?php
$a['date'] = mktime();
$a11 = date("F j, Y, g:i a", $a['date']);
$newTime = date(strtotime('+3 hour'),$a['date']);
$b11 = date("F j, Y, g:i a", $newTime);
echo $a11 . " AND " . $b11;
?>
Dig it, you are not strtotime'ing the $newTime when converting to date, so it's false.
<?php
$a['date'] = time();
$a11 = date("F j, Y, g:i a", $a['date']);
echo 'Now = ' . time() . PHP_EOL;
echo 'Now +3hrs = ' . strtotime( '+3 hours' ) . PHP_EOL . PHP_EOL;
$newTime = strtotime( '+3 hours' );
$b11 = date("F j, Y, g:i a", $newTime );
echo $a11 . ' and ' . $b11 . PHP_EOL;
The format of date function is: string date ( string $format [, int $timestamp ] ). So, according to the first line, $a['date'] stores the timestamp value. But, according to the second line, its value is date format.
Moreover, you should type "+3 hours".
I add date like following
<?php
$a['date']="March 22, 2011, 10:22 pm";
$a11 = date("F j, Y, g:i a", strtotime($a['date']));
$b11 = strtotime(date("F j, Y, g:i a", strtotime($a['date'])) . " +3 hours");
$b11 = date("F j, Y, g:i a", $b11);
echo $a11 . "AND " . $b11;
?>