How to display weekly(Mon to Sun) data in php and mysql? - php

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

Related

Get the last day of the next 6 months prior to the current date

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;

Calculate maximum week number in PHP [duplicate]

This question already has answers here:
PHP - get last week number in year
(7 answers)
Closed 7 years ago.
I am searching for a way to calculate the maximum week number for a given year in PHP. Since the last week number of a year is defined by whichever week has the first Thursday in the new year, I am not really sure how to do it.
date('W', strtotime( $year.'-12-31 23:59:59'));
could also already return 1, if the 31st of December is a Monday, Tuesday or Wednesday. So I was thinking, maybe do it like this?
date('N', strtotime( $year.'-12-31 23:59:59')) <= 3 ? 52 : 53;
i.e., checking if the last day of the year is a Monday, Tuesday or Wednesday and if so, it's 52 week year, otherwise a 53 week year. Not sure if that's the correct way.
To get the ISO week number (1-53), use :-
idate('W', $timestamp)
or use this :-
$date1 = "2015-12-12";
$date = new DateTime($date1);
$week = $date->format("W");
echo "total week: $week";
or try this :-
date("W", strtotime('2015-12-12'))

Get day of the week for last 7 days?

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);
}

getting start and end dates of ther week that precedes the given date [duplicate]

This question already has answers here:
Get Start and End Days for a Given Week in PHP
(16 answers)
Closed 9 years ago.
I am selecing a date from jquery datepicker and I want to pick the starting and ending date of the week that precedes this date. - for example picking April 10, 2013 should return March 31, 2013 and April 6, 2013.
Sunday is the first day of the week and Saturday is last.
Below is my code.
$weekday = $d->format('w');
$diff = 7 + ($weekday == 0 ? 6 : $weekday - 0); //to make week start from monday till sunday add 1 to weekday -
$start1 = $d->modify("-$diff day");
$start_date = $d->format('Y-m-d');
$end1 = $d->modify('+6 day');
$stop_date = $d->format('Y-m-d');
The code works fine, at least mostly. However, when I pick any sunday I get a wrong a non expected result which is the previous week of the previous week.
for example, if I select March 31, 2013 - I should get march 24 and march 30,, but I am getting march 28 and march 24.
Where am I going wrong ?
Try this, it should solve your problem:
$input = 'April 10, 2013'; // come from jquery
$dt = new DateTime($input . ' -1week');
$monday = clone $dt->modify(('Sunday' == $dt->format('l')) ? 'Monday last week' : 'Monday this week');
$sunday = clone $dt->modify('Sunday this week');
printf("You've selected a date in the week from %s to %s\n",
$monday->format('Y-m-d'),
$sunday->format('Y-m-d')
);

[mysql&php], what is the first day of a week, Monday or Sunday?

What is the first day of the week in mysql, Monday or Sunday??
I want to do this: (i am using php btw)
Get today date (no problem)
Display information of this week (stuck here)
Previous and next week button, to display previous and next week data (cant do anything here)
I am kinda of stuck while playing with the "date" thing. Can anyone help?
Monday.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_weekday
WEEKDAY(date)
Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
To get details about previous or next weeks you can use
DATE_ADD(date,INTERVAL expr unit), DATE_SUB(date,INTERVAL expr unit)
eg SELECT DATE_ADD('2010-12-31 23:59:59', INTERVAL 1 WEEK);
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
To get the weekday in PHP you can use the following:
<?php
$datetime = new DateTime('2010-12-31 23:59:59');
$today = $datetime->format('l');
echo $today;
?>
http://www.php.net/manual/en/function.date.php

Categories