Hướng dẫn php weekday index

I want to get Monday, Tuesday, Wednesday... using php function.

I have only numeric values for those like 1,2,3..7 where

1 = Monday 2 = Tuesday ... 7 = Sunday.

Can anybody help me regarding this.

Thanks,

Kanji

asked Dec 29, 2010 at 6:07

6

My personal favorite...

$day_start = date[ "d", strtotime[ "next Sunday" ] ]; // get next Sunday
for [ $x = 0; $x < 7; $x++ ]
    $week_days[] = date[ "l", mktime[ 0, 0, 0, date[ "m" ], $day_start + $x, date[ "y" ] ] ]; // create weekdays array.

Khantahr

7,9084 gold badges33 silver badges55 bronze badges

answered Dec 8, 2012 at 19:21

CarlosCarlos

1601 gold badge2 silver badges13 bronze badges

If all you have is a numeric value and not a complete timestamp, by far the easiest way is this:

$weekdays = array['Monday', 'Tuesday', 'Wednesday', ...];
$weekday = 0;
echo $weekdays[$weekday];

answered Dec 29, 2010 at 6:14

decezedeceze

497k81 gold badges718 silver badges866 bronze badges

DateTime::format, with the $format parameter as l [lowercase L].

Object Oriented style:

$object->format['l'];

Procedural style:

date_format[DateTime $object, 'l'];

You can create a DateTime object with DateTime::__construct, and you can learn more about DateTime formats here.

answered Dec 29, 2010 at 6:12

waiwai933waiwai933

13.7k21 gold badges62 silver badges84 bronze badges

3

The following should give you the day of the week for today [ex. tuesday]:

or for a specific date you can use something like this:

For more info: //www.php.net/manual/en/function.date.php

If you have just a number that you are trying to convert to a day of the week, you could use the following:

function convertNumberToDay[$number] {
    $days = array['Sunday','Monday', 'Tuesday', 'Wednesday','Thursday','Friday','Saturday'];
    return $days[$number-1]; // we subtract 1 to make "1" match "Sunday", "2" match "Monday"
}
echo convertNumberToDay[3]; // prints "Tuesday"

answered Dec 29, 2010 at 6:28

Andy FlemingAndy Fleming

7,3945 gold badges32 silver badges53 bronze badges


So you can create a simple function like this:

function getDayName[$dayNo] {
    return date['l', mktime[0,0,$dayNo,0,1]];
}

Demo: //www.ideone.com/hrITc

answered Dec 29, 2010 at 6:29

karim79karim79

336k66 gold badges410 silver badges405 bronze badges

If you simply want to convert 0 to "Monday", 1 to "Tuesday", etc. use an array.

$day_of_week = array
[
    0 => 'Monday',
    1 => 'Tuesday',
    2 => 'Wednesday',
    .
    6 => 'Sunday'
];

$day = 2;

echo $day_of_week[$day];

answered Dec 29, 2010 at 6:46

Amil WaduwawaraAmil Waduwawara

1,6121 gold badge16 silver badges14 bronze badges

1

switch [$day] {
    case 0 : echo "Monday"; break;//if $day = 0  it will print Monday
    case 1 : echo "Tuesday"; break;
    case 2 : echo "Wednesday"; break;
}

answered Dec 29, 2010 at 6:15

rajmohanrajmohan

1,5981 gold badge13 silver badges32 bronze badges

How to create unique id php?

❮ PHP Misc ReferenceDefinition and UsageThe uniqid[] function generates a unique ID based on the microtime [the current time in microseconds].Note: The generated ID from this function does not ...

Hướng dẫn dùng php execute trong PHP

Sử dụng pdo trong php trong php bài bài viết hướng dẫn bạn cách dùng thư viên PDO của php để thao tác với dữ liệu trong database.Nội dung chính2. Kết nối đến ...

Hướng dẫn dùng define destruct trong PHP

This is the simplest type. A bool expresses a truth value. It can be either true or false. Syntax To specify a bool literal, use the constants true or false. Both are case-insensitive. ...

Php convert week number to date

Im trying to group together dates into a week number and year, and then I want to convert that week number back into a unix timestamp. How can I go about doing this? asked Apr 23, 2011 at 9:10 1I ...

Hướng dẫn function curl php

Giới ThiệuCURL là bộ thư viện được sử dụng để giúp thực hiện việc chuyển dữ liệu thông qua nhiều giao thức khác nhau [như HTTP, FPT...]. Với giao thức ...

How do i scan a value in php?

View DiscussionImprove ArticleSave ArticleReadDiscussView DiscussionImprove ArticleSave ArticleIn PHP, the console is a command-line interface, which is also called interactive shell. We can access ...

What are the file modes in php?

View DiscussionImprove ArticleSave ArticleReadDiscussView DiscussionImprove ArticleSave ArticleFile handling is needed for any application. For some tasks to be done file needs to be processed. File ...

Hướng dẫn php 8.0 zip extension

Làm sao để thực hiện nén file .zip bằng PHP, bài viết này sẽ giới thiệu và hướng dẫn các bạn thực hiện nén file .zip bằng PHP một cách đơn giản.Nội dung ...

Php format price with commas

In my database I have values like256.23, 200.33, 89.33, 133.45,I have to multiply these values with thousand and then format the result as price[comma separated]256.23 x 1000 = 256230 I ...

Hướng dẫn dùng de echo trong PHP

Hàm echo[] trong PHP được dùng để hiển thị dữ liệu ra màn hình.Cú phápecho strings;

Chủ Đề