Is it possible to get the name of the day in the row of numbers converted to date?
I have Code :
$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));
for($Date = 1; $Date <= $Maximum_Date; $Date++){
$DataDate = $Date . ' ' . $Month . ' ' . $Year . '<BR>';
echo $DataDate;
}
Result :
1 04 2019
2 04 2019
3 04 2019
etc..
What I want is to change it to display the name of the day on that date
For Example Date in April :
Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
etc..
[UPDATE] April 15, 2019
Refer to comments, I see documentation here and apply with mktime();
So I Update The Code :
$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));
for($Date = 1; $Date <= $Maximum_Date; $Date++){
echo date("l, d m Y", mktime(0, 0, 0, $Month, $Date, $Year)) . '<br>';
}
And get the result :
Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
etc..
You can simplify this a lot without converting back and forth between date and strtotime:
$year = date('Y');
$month = date('n');
$lastDay = date('t');
foreach (range(1, $lastDay) as $day) {
echo date('D, j m Y', mktime(0, 0, 0, $month, $day, $year)), '<br>';
}
See http://php.net/mktime.
Omitting implicit default values and condensing it a bit, you can in fact boil it down to:
foreach (range(1, date('t')) as $day) {
echo date('D, j m Y', mktime(0, 0, 0, date('n'), $day)), '<br>';
}
Mon, 1 04 2019
Tue, 2 04 2019
Wed, 3 04 2019
...
Tue, 30 04 2019
Note that this code has a minuscule potential to break, should you execute it right at the second in which one month rolls over to the next, and the date('n') and date('t') functions happen to be called "in different months". To avoid that possibility entirely, make this operation atomic:
list($year, $month, $lastDay) = explode(' ', date('Y n t'));
foreach (range(1, $lastDay) as $day) {
echo date('D, j m Y', mktime(0, 0, 0, $month, $day, $year)), '<br>';
}
The code below will print out what you need..
$date1 = date('Y-m-01');
$Maximum_Date = date('t', strtotime($date1));
for($Date = 1; $Date <= $Maximum_Date; $Date++)
{
$thatDay = date('Y-m-'.$Date);
$day = date('l, j', strtotime($thatDay));
$Month = date('m', strtotime($thatDay));
$Year = date('Y', strtotime($thatDay));
echo $day . ' ' . $Month . ' ' . $Year . '<BR>';
}
Output:
Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
Thursday, 4 04 2019
Friday, 5 04 2019
...
...
if you change this line
$day = date('l, j', strtotime($thatDay));
TO
$day = date('l, jS', strtotime($thatDay));
The output will be: Monday, 1st 04 2019
Use:
$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));
for($Date = 1; $Date <= $Maximum_Date; $Date++){
$DataDate = date("D-m-Y",strtotime($Year."-".$Month."-".$Date));
echo $DataDate. '<br>';;
}
You should try like below.
$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));
$StartDate=$date1;
$EndDate=($Year."-".$Month."-".$Maximum_Date);
$EndDate=date('Y-m-d',strtotime($EndDate));
while(strtotime($EndDate)>=strtotime($StartDate))
{
echo date('l, d-m-Y',strtotime($StartDate))."<br/>";
$StartDate=date('Y-m-d', strtotime($StartDate . ' +1 day'));
}
After execute you will get result like below.
Monday, 01-04-2019
Tuesday, 02-04-2019
Wednesday, 03-04-2019
...................
...................
...................
...................
Saturday, 27-04-2019
Sunday, 28-04-2019
Monday, 29-04-2019
Tuesday, 30-04-2019
Add the following line to your loop:
echo (new DateTime($Date. '-'. $Month .'-'. $Year))->format('l');
It creates a new DateTime object with your data & echo's the day in the format that you requested.
Related
This give me every monday date in date range.
Question: How to get every monday and friday of week?
$start_date = date('Y-m-d');
$end_date = date('Y-m-d', strtotime($start_date . ' + 1 MONTH'));
for(
$i = strtotime('Monday', strtotime($start_date));
$i <= strtotime($end_date);
$i = strtotime('+1 WEEK', $i)
) {
echo date('Y-m-d', $i). '<br>';
}
My Update:
$my_dates = [];
for(
$i = strtotime($start_date);
$i <= strtotime($end_date);
$i = strtotime('+1 DAY', $i)
) {
if(in_array(date('N', $i), array(1, 5))) {
$my_dates[] = date('Y-m-d', $i);
}
}
var_dump($my_dates);
Have a look at library called When, it's "Date / Calendar recursion library for PHP 5.3+".
Let's say MF schedule for next month:
$now = new DateTime('NOW');
$till = clone $now;
$till->modify('+1 month');
$r = new When();
$r->startDate($now)
->freq("weekly")
->until($till)
->byday(array('MO', 'FR'))
->generateOccurrences();
$occurrences = $r->occurrences;
If I'm not wrong than you can simply use for loop like as
$start = "2015-09-01";
$end = date('Y-m-d', strtotime("$start +1 months"));
$period = floor((strtotime($end) - strtotime($start))/(24*60*60));
for($i = 0; $i < $period; $i++){
if(in_array(date('l',strtotime("$start +$i day")),["Monday","Friday"]))
echo date('l d M, Y',strtotime("$start +$i day"))."\n";
}
Output:
Friday 04 Sep, 2015
Monday 07 Sep, 2015
Friday 11 Sep, 2015
Monday 14 Sep, 2015
Friday 18 Sep, 2015
Monday 21 Sep, 2015
Friday 25 Sep, 2015
Monday 28 Sep, 2015
Demo
Suppose I have 2 dates say 29 Aug 2014 and 3 Sep 2014. I need to display all dates between these to dates in the below format.
Aug 2014
29 Fri
30 Sat
31 Sun
Sept 2014
01 Mon
02 Tue
03 Wed
I know how to print all the dates like 29,30,31,1,2,3. But what I am unable to do is to get the month names in between.
Pretty easy question to be honest, pretty basic sollution possible..
$dateRange = new DatePeriod(
new DateTime('2014-07-28'),
new DateInterval('P1D'),
new DateTime('2014-08-04 00:00:01')
);
$month = null;
foreach ($dateRange as $date)
{
$currentMonth = $date->format('m Y');
if ($currentMonth != $month)
{
$month = $date->format('m Y');
echo $date->format('F Y').'<br />';
}
echo $date->format('d D').'<br />';
}
Above sollution results in:
July 2014
28 Mon
29 Tue
30 Wed
31 Thu
August 2014
01 Fri
02 Sat
03 Sun
Do mind it needs PHP >= 5.3 (due to the use of DatePeriod), but the actual logic to solve your question is easy to implement regardless of the used PHP version.
$timeS = strtotime("29 Aug 2014");
$timeE = strtotime("3 Sep 2014");
$monthS = -1;
$time = $timeS;
while ($time < $timeE) {
if ($monthS != date("n", $time)) {
echo date("M Y", $time) . "\n";
$monthS = date("n", $time);
}
echo date("d D", $time) . "\n";
$time = strtotime("+1 day", $time);
}
Edit: After doing it I'm pretty ok with #hindmost comment :)
I think , this is the complete code , as you wanted.
Executed code is here...
http://phpfiddle.org/main/code/3cbe-4855
<?php
$currentMonth = null;
$timeS = strtotime("29 Aug 2013");
$timeE = strtotime("3 Sep 2014");
$time = $timeS;
while ($time < $timeE) {
$month = date("M", $time);
$year = date("Y", $time);
if ($month != $currentMonth)
echo "<br /><h3>".$month."- ".$year."</h3>";
$currentMonth = $month;
echo "<br />".date("d D", $time);
$time = strtotime("+1 day", $time);
}
?>
In my script, I have a given end date. To get the start date, I subtract 23 months to the end date. Basically, what my script should do is to output 24 months (w/ year) - the last month/year to be printed should always be the specified end date.
For some reason, my script isn't returning my desired results. Given the $end = '2013-07-05', the script returns the result correctly. It prints out Aug 11 to Jul 13 which is correct.
But for some dates (e.g. $end = '2013-07-31'), the output is wrong. The result should be Sep 11 to Aug 13. But in this case, it outputs Aug 11 to Aug 13 which is absolutely wrong.
Here's my code:
<?php
$end = strtotime('2013-07-31 +1 month');
$date = strtotime('2013-07-31 -23 month');
$start = $month = $date;
$months = "";
while($month < $end)
{
$months .= date('M y', intval($month))." ";
$month = strtotime("+1 month", intval($month));
}
echo $months;
?>
I think there's something wrong with strtotime(). Thanks in advance.
You can't really use month calculations like that, especially when dealing with end-of-month values:
e.g. if it's July 31, what's -1 month to strtotime?
php > echo date('r', strtotime('2013-07-31 -1 month'));
Mon, 01 Jul 2013 00:00:00 -0600
A human would probably pick out June 30th, but strtotime isn't human. This DOES work for February 28th and generally any date where the day value is <= 28. Once you get into the 29,30,31 area, then you get these unexepected results
php > echo date('r', strtotime('2013-04-28 -1 month'));
Thu, 28 Mar 2013 00:00:00 -0600
How about
$endMonth = '8';
$year = '2013';
$i = 24;
while( $i > 0 ){
$month = ($endMonth - $i)%12;
if( $month == 0 ){
$year = $year - 1;
$month = 12;
}
$months .= date('M y', strtotime($year.'-'.$month.'-02'));
$i--;
}
Based on Marc B's answer I modified the script to deal with the 29,30,31 of each month. What I did was, if the date is 29, 30, or 31, it will be subtracted with 3 days so that the date will be either 28 or below and would work just fine with the current code that I have. It worked for me so I guess I'll just stick with this for now. Here's the updated code:
<?php
$dt = "2013-07-31";
$dy = strtotime($dt);
$day = date("d", $dy);
if (($day == 29) || ($day == 30) || ($day == 31)){
$dt = strtotime("$dt -3 days");
$dt = date('Y-m-d', $dt);
}
$end = strtotime("$dt +1 month");
$date = strtotime("$dt -23 month");
$start = $month = $date;
$months = "";
while($month < $end)
{
$months .= date('M y', intval($month))." ";
$month = strtotime("+1 month", intval($month));
}
echo $months;
?>
Thanks for your help and insights. :)
how to get the previous 3 months in php ex(If i say DEC.. It should display the previous 3 months i.e., OCT NOV DEC)
You can use the strtotime function like this:
echo date('M', strtotime('-3 month'));
So you specify previous dates with minus sign.
echo date('M', strtotime('0 month'));
echo date('M', strtotime('-1 month'));
echo date('M', strtotime('-2 month'));
echo date('M', strtotime('-3 month'));
Results:
Dec
Nov
Oct
Sep
You can do the same if you are using a loop like this:
for ($i = -3; $i <= 0; $i++){
echo date('M', strtotime("$i month"));
}
Results:
Sep
Oct
Nov
Dec
Check out the documentation too see many other friendly date and time keywords strtotime supports:
http://php.net/manual/en/function.strtotime.php
Voted answer is almost correct.
Correct solution is:
for ($i = -3; $i <= 0; $i++){
echo date('M', strtotime("$i month", strtotime(date("Y-m-15"))));
}
Explain: strtotime default implementation is:
date ( string $format [, int $timestamp = time() ] ) : string
As you can see there is timestamp with default value of: time().
Last say today is 31th March.
Because there is no 31th Feb
echo date('M', strtotime("-1 month"));
will return March
On the other hand:
echo date('M', strtotime("+1 month"));
will return May (April will be skipped).
Because of that issue we have to setup timestamp value which is a safe date.
Every date between 1-28 is safe because every month have got that date. In my example I had choose 15th day of month.
$month = date('M');
$year = date('Y');
$no_of_mnths = date('n',strtotime("-2 months"));
$remaining_months = (12 - $no_of_mnths)."\r\n";
$tot = $remaining_months+1;
for($j=0;$j<$tot;$j++){
echo date('M',strtotime(''.$no_of_mnths.' months'))
$no_of_mnths++;
}
This may help you`
<?php
$date = explode("-", "2016-08-31");
if($date[2]== "01"){$days = "-0 days";}else{$days = "-1 days";}
$time = strtotime($days, mktime(0, 0, 0, $date[1], $date[2], $date[0]));
$MTD = date('Y-m-01', strtotime('0 month'));
$M1 = date('Y-m-01', strtotime('-1 month'));
$M2 = date('Y-m-01', strtotime('-2 month'));
$M3 = date('Y-m-01', strtotime('-3 month'));
$rng = array();
$rng[$MTD] = strftime("%B, %Y", strtotime(" ", $time));
$rng[$M1] = strftime("%B, %Y", strtotime("first day of previous month", $time));
$rng[$M2] = strftime("%B, %Y", strtotime("-2 months", $time));
$rng[$M3] = strftime("%B, %Y", strtotime("-3 months", $time));
?>
Here $MTD ,$M1,$M2,$M3 will give date in the form of "dd-mm-yyyy" and $rng[$MTD], $rng[$M1],$rng[$M2],$rng[$M3] give date in the form of"Name of Month,year" => (i.e August,2016)
How can I get the Sunday and Saturday of the week given a specific date?
For example:
input: Monday, September 28, 2009
output should be:
Sunday, September 27, 2009 12:00 AM - Saturday, October 3, 2009 11:59 PM
I am thinking of using the date, strtotime, mktime and time php functions.
If you have a usable function then it is also fine with me.
Thanks in advance :)
Cheers,
Mark
You use strtotime() and date():
<?php
$s = 'Monday, September 28, 2009';
$time = strtotime($s);
$start = strtotime('last sunday, 12pm', $time);
$end = strtotime('next saturday, 11:59am', $time);
$format = 'l, F j, Y g:i A';
$start_day = date($format, $start);
$end_day = date($format, $end);
header('Content-Type: text/plain');
echo "Input: $s\nOutput: $start_day - $end_day";
?>
outputs:
Input: Monday, September 28, 2009
Output: Sunday, September 27, 2009 12:00 PM - Saturday, October 3, 2009 11:59 AM
<?php
date_default_timezone_set('Europe/London');
$datetime = new DateTime("2009-09-23 12:00:00");
// Saturday
$datetime->setISODate(2009, $datetime->format("W"), 6);
print "Saturday:" . $datetime->format(DATE_ATOM) . "\n";
// Sunday
$datetime->setISODate(2009, $datetime->format("W"), 0);
print "Sunday: " . $datetime->format(DATE_ATOM) . "\n";
?>
take a look at strtotime
i.e.
strtotime('next sunday', strtotime($your_date_string));
echo date("Y-m-d H:i:s", strtotime("last sunday", time()));
echo date("Y-m-d H:i:s", strtotime("next saturday", time()));
the issue with the 'correct' answer is, what if the supplied date is Sunday? Then it will give you the previous Sunday, instead of the current Sunday. Same issue with Saturday.
$s = 'Monday, September 28, 2009';
$time = strtotime($s);
if(date('D', $time) == "Sun"){
$start = strtotime(' 12pm', $time);
}
else {
$start = strtotime('last sunday, 12pm', $time);
}
if(date('D', $time) == "Sat"){
$start = strtotime(' 12pm', $time);
}
else {
$start = strtotime('next saturday, 12pm', $time);
}
$format = 'l, F j, Y g:i A';
$start_day = date($format, $start);
$end_day = date($format, $end);
header('Content-Type: text/plain');
echo "Input: $s\nOutput: $start_day - $end_day";
strtotime
$input = strtotime("Monday, September 28, 2009");
$nextSunday = strtotime("next Sunday",$input);
<?php
$date = strtotime('Monday, September 28, 2009 - 1 day');
$initialString = date('l, F d, Y g:i A', $date);
$end = date('l, F d, Y g:i A', strtotime( 'next saturday 11:59 pm', $date));
echo $initialString . ' - ' . $end;
output:
Sunday, September 27, 2009 12:00 AM - Saturday, October 03, 2009 11:59 PM
This is what your looking for. It is what I use.
$beginningOfWeek = date('Y-m-d H:i:s', strtotime('last Sunday'));
$endOfWeek = date('Y-m-d H:i:s', strtotime('+6 day', strtotime('last Sunday')) );
You may find my Date Time Helper much handy in this case , it can finish in 3 Lines
http://normandqq.github.io/Date-Time-Helper/
$date = '2013-08-11';
$monday = Model_DTHpr::getMondayByDate($date);
$sunday = Model_DTHpr::adjustDays("add",$monday,6);
Out Put:
2013-08-05 //Monday
2013-08-11 //Sunday
Get Sunday Php Code:
echo "Today Is : " . date("d-m-Y");
$dw = date( "w");
$dw = 7-$dw;
echo "\nNext Sunday Is" .date('d-m-Y', strtotime("+$dw days"));