I need the names of the day (Monday, Tuesday, Wednesday, Thuesday, Friday, Saturday, Today).
I know this is a newby question and PHP has a date() function. But I tried and can't figure out how...
According to the PHP Manual at http://php.net/manual/en/function.date.php, just use "l" as the format parameter to get the full name of the day.
So 23rd Mar 2014 is a Sunday, as echoed by
<?php
echo date ("l", mktime(0, 0, 0, 3, 23, 2014));
// Echoes Sunday
?>
To get past 7, 6, 5 or 10000 days (or number of days in the future) from the current day, according the information at this page, just use negative or positive integers in a string in the strtotime function:
<?php
$backcount = -4;
echo date ("l", strtotime("$backcount day"));
// Executed on 23 Mar 2014 will give Wednesday
?>
Knowing this, you can apply a for loop to get what you need. And if want "Today" instead of the full name of the current day, just add an if condition to handle the situation where the backcount variable is zero.
Achieving this using the DateTime Class and its format method.
The below code's output changes every day.. Since today is Sunday it starts from Monday , Tuesday... If you run this code on Tuesday , you will be getting output as Thursday , Friday , Saturday .. so on.
<?php
for($i=1;$i<=7;$i++) //<--- Since we know total days in a week is 7.
{
$date = new DateTime(); //<-- Grabs today's datetime
$date->add(new DateInterval('P'.$i.'D')); //<--- Passes the current value of $i to add days..
echo $date->format('l')."<br>";
}
OUTPUT :
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Working Demo
you can use jddayofweek to retrieve day of week, it has 3 mode for string containing the day of week, string containing the abbreviated day of week and int represent number of day in week
for($i=0;$i<7;$i++){
$x=jddayofweek($i,2);
var_dump($x);
}
Related
How do I find out the number of days until the next January 1 without specifying a year?
I always want to know how many days until the next January 1, so I don't want to say "how many until Jan 1, 2020" or I'll need to reset my variable every year.
You can use DateTime objects to do the math. Create one object which is today, and another which is January 1, to which we then add a year, and then take the difference:
$today = new DateTime();
$jan1 = new DateTime('January 1');
$jan1->modify('+1 year');
$days = $today->diff($jan1)->days;
echo "$days days until January 1\n";
Output (on December 6)
25 days until January 1
Demo on 3v4l.org
I have problem with my code that gets the last day of the next 6 months. My code gets first the last day of the current month then using the date function + 6 months.
$lastday = date("Y-m-t");
$lastdayaftersixmonth = strtotime("$lastday +6 months");
But my problem is when it comes to months that do not have same number of days specifically in case of august and february. If the last day for august is 31, supposedly it must return february 28 (or 29 for leap year), but it returns March 2.
$date = date('Y-m-t',strtotime('+6 month'));
echo $date;
Ok, So im trying to figure out the best way in php to write (or print) the 1st, 2nd, 3rd, 4th, & 5th date of the current week (starting with Monday) in php.
For example... Using the week of the 4th through the 8th of February...
I would need a script for the 1st day of the week (starting with Monday) displayed as February 04, 2013 and then I would need the same script four more times to display Tues, Wed, Thurs, & Fri...
All-together I would end up with 5 scripts or one script that I could copy and manipulate to work the way I need it to...
Also, If you could tell me how to do the same for Saturday and Sunday that would be greatly appreciated as-well.
If there is anything that you do not understand, please let me know and I will try my hardest to clarify...
Thanks in advance!
First you need to get the "current" week's Monday. To do this, I would suggest calling date("N") and see if it's 1. If it is, then now is the Monday you want. Otherwise last monday is. Pass that to strtotime to get the timestamp corresponding to the first monday of the week. Then repeatedly add 24 hours (24*3600 seconds) to get each day.
$startofweek = date("N") == 1 ? time() : strtotime("last monday");
for($i=0; $i<5; $i++) {
$day = $startofweek + 24*3600*$i;
echo "Day ".($i+1).": ".date("d/M/Y",$day)."<br />";
}
You can use strtotime with special parameters. like:
$time = strtotime('monday this week');
$time = strtotime('today');
$time = strtotime('next monday');
$time = strtotime('previous monday');
Same goes for every other days of the week
I'm trying to get the unix time for date strings that are formatted like so:
'second sunday of march 2010'
'first sunday of november 2010'
I was under the impression that strtotime could handle such a string, but apparently not, as this returns false. How can I convert to unix time when given a day of week, which one of those in the month (ie. first, second, etc.), a month and a year.
This should be possible with strtotime. You could try generating a timestamp of the first day of march using mktime() and adding that as a 2nd parameter (leaving just "first sunday" in the string part):
$timestamp = mktime (0,0,0,3,1,2010); // 1st of march
$first_sunday = strtotime("first sunday", $timestamp);
Not sure how this will handle the first day (March 1st) actually being a sunday. Make sure you check that out.
Also, and maybe this more reliable, check this out - the poster says he got good results with the following notation (quoting):
<?php
strtotime('+0 week sun nov 2009'); // first sunday in nov 2009
strtotime('+1 week sun nov 2009'); // second sunday
strtotime('-1 week sun nov 2009'); // last sunday in oct 2009
?>
As always with strtotime, whatever you pick, make sure you test well, especially the edge cases (1st day of month is a sunday, last day of last month was a sunday....)
Your code works for me on PHP 5.3.0. What version of PHP are you using?
<?php
date_default_timezone_set("Europe/Oslo");
$time_march = strtotime('second sunday of march 2010');
$time_november = strtotime('first sunday of november 2010');
echo date("Y-m-d", $time_march) . " (timestamp: $time_march)\n";
echo date("Y-m-d", $time_november) . " (timestamp: $time_november)\n";
?>
gives:
2010-03-14 (timestamp: 1268521200)
2010-11-07 (timestamp: 1289084400)
In php i can get today date/day by using : $today = date('D, Y-m-d');
If today is wednesday 8 july, i want to display data from 6 - 11 July (9, 10 and 11 will be blank). How do I do that?
Now my system is like, today is 8 July, i will display 2 - 8 july, but start on wednesday. Not very like this way. I prefer, start on Monday to sunday, then have previous and next week button.
Use simple strtotime() to easy calculate the date of last monday.
$lastMonday = strtotime('last monday');
Be sure to check if today is not monday, because in that case it will return a date week earlier.
... WHERE YEARWEEK( $inputdate ) = YEARWEEK( datefield) ...
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_yearweek