tags:

views:

434

answers:

1

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?

+2  A: 

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

Josh
I need to find out the weekday from today date which get from php function, not database(sql).
jingleboy99
so where does the mysql come into the question?
Josh
I appreciate your cool method. But, i cant use mysql function in php. E.g. user input today date, and pass it to php. My php cant use the weekday function to know the index of the date. Or am i wrong?
jingleboy99
Sorry, I need to change my question title then.. really sorry
jingleboy99
the point is your question talks about mysql - it isn't very clear
Josh
Er, there is another way to get weekday in php. $day = date('D'); Then from this weekday, how to display this week data? I need to check whether today is monday, etc.. If i do this way, there are alot of calculation to do here, so i am asking is there any easier way to display weekly data? I should post another question with title: "display weekly data". Thanks anyway for the sql function. I learned something new today
jingleboy99