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: http://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: http://www.ideone.com/hrITc

answered Dec 29, 2010 at 6:29

Hướng dẫn php weekday index

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

Hướng dẫn php weekday index

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 php weekday index

Hướng dẫn php weekday index

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 php weekday index

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. ...

Hướng dẫn php weekday index

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 php weekday index

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 ...

Hướng dẫn php weekday index

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 ...

Hướng dẫn php weekday index

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 weekday index

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 ...

Hướng dẫn php weekday index

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 php weekday index

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;; echo Tài ...

Hướng dẫn php weekday index

How check file is image or not in php?

function isImage($image){ $extension = pathinfo($image, PATHINFO_EXTENSION); $imgExtArr = [jpg, jpeg, png]; if(in_array($extension, $imgExtArr)){ return true; } ...

Hướng dẫn php weekday index

Generate random string in php without repetition

Im trying to write a PHP function which will generate supposedly random strings which need to be unique regardless of the number of times it is run. Well, it can run more than once in order to ...

Hướng dẫn php weekday index

Hướng dẫn dùng edeca login trong PHP

I. Giới thiệu:Sau khi các bạn đã học các kiến thức cơ bản về PHP và MySQL thì trong bài này mình sẽ hướng dẫn các bạn xây dựng chức năng đăng nhập và ...

Hướng dẫn php weekday index

Hướng dẫn php slice

Định nghĩa hàm array_slice() trong PHPHàm array_slice() trong PHP trả về dãy phần tử từ mảng array khi được xác định bởi các tham số offset và length.Nội dung ...

Hướng dẫn php weekday index

Hướng dẫn php weekday index

Hướng dẫn php continue running script

I want to write a script which calls some code but continues to run after calling it. E.gfunction foo() { $this->load_function_a(); $this->load_function_b(); echo A and B succesfully ...

Hướng dẫn php weekday index

Hướng dẫn install php on mac

Trong macOS phiên bản mới (như 10.11, 10.12, 10.13 ...) có cài sẵn mặc định Webserver Apache HTTP và PHP, tuy nhiên để dễ tuỳ biến trong phần này sẽ huỷ sử dụng ...

Hướng dẫn php weekday index

How do i uninstall php 7.3 on centos 7?

First remove everything regarding old php version (will take some time...) sudo yum remove --setopt=clean_requirements_on_remove=1 php php-pear php-mysql php-cli php-common mod-php then, install ...

Hướng dẫn php weekday index

Hướng dẫn php openlog

❮ Tham chiếu mạng PHPThí dụMở và đóng kết nối của trình ghi nhật ký hệ thống:

Hướng dẫn php weekday index

Hướng dẫn oops concepts in php

Khi mới làm quen với lập trình chúng ta thường bắt đầu với các ngôn ngữ như Pascal, C là những ngôn ngữ lập trình cấu trúc với việc thực hiện mã lệnh ...

Hướng dẫn php weekday index

Is zero considered empty php?

(PHP 4, PHP 5, PHP 7, PHP 8)empty — Determine whether a variable is emptyDescriptionempty(mixed $var): boolParameters var Variable to be checked No warning is generated if the variable does not ...

Hướng dẫn php weekday index

How to open php file on android

How do I open a PHP file on Android? – Related QuestionsHow do I open a PHP file in Chrome?How do I open a PHP file online?How do I open PHP files in PDF?How do I run a PHP file on localhost?How do ...

Hướng dẫn php weekday index

Hướng dẫn dùng open dir trong PHP

Giới thiệuTrong phát triển ứng dụng, nhiều khi chúng ta cần làm việc với file (tập tin) và folder (thư mục). Một số thao tác phổ biến có thể kể đến như ...

Hướng dẫn php weekday index

What is function definition in php?

A function is a piece of code that takes another input in the form of a parameter, processes it, and then returns a value. A PHP Function feature is a piece of code that can be used over and over ...

Hướng dẫn php weekday index

Hướng dẫn what is php variable

Bài này sẽ giới thiệu biến (variable) và hằng (constant) trong PHP. Để học tốt bài này, các bạn cần đọc lại bài Cài đặt môi trường lập trình Web PHP với ...

Hướng dẫn php weekday index

Hướng dẫn dùng decode uri trong PHP

Thỉnh thoảng, bạn sẽ phải chuyển các URL giữa các trang net và dịch vụ khác nhau. Nghe có vẻ như một nhiệm vụ khá dễ dàng vì URL về cơ bản chỉ là các ...

Hướng dẫn php weekday index

Hướng dẫn dùng pkey trong PHP

Hàm key() sẽ lấy ra khóa của phần tử hiện tại trong mảng.Nội dung chính2. Định nghĩa2. Cú pháp3. Các loại mảng3.1. Mảng số nguyên3.2. Mảng liên hợp3.3. ...

Hướng dẫn php weekday index

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

Trang chủHướng dẫn họcHọc PHPPHP xử lý formPHP xử lý formCác giá trị thành phần của form được thu thập thông qua phương thức _GET và _POST.Các giá trị thành ...

Hướng dẫn php weekday index

Hướng dẫn php compress output

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 ...

Hướng dẫn php weekday index

Hướng dẫn dùng fetcj trong PHP

Nếu bạn là một PHP Developer, chắc hẳn bạn đã rất quen thuộc với việc truy xuất Database (Cơ sở dữ liệu) bằng các extensions MySQL và MySQLi. Từ PHP 5.1 ta có ...

Hướng dẫn php weekday index

Hướng dẫn php catch multiple exceptions

Exceptions are used to change the normal flow of a script if a specified error occurs.Nội dung chínhWhat is an ExceptionBasic Use of ExceptionsTry, throw and catchExample explained:Creating a ...

Hướng dẫn php weekday index

Hướng dẫn php weekday index

Hướng dẫn php curl test online

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 ...

Hướng dẫn php weekday index

Hướng dẫn php weekday index

Hướng dẫn php weekday index

Hướng dẫn dùng alphanumeric order trong PHP

(PHP 4, PHP 5, PHP 7, PHP 8)rand — Génère une valeur aléatoireDescriptionrand(): intrand(int $min, int $max): int Appelée sans les options min et max, rand() retourne un nombre pseudoaléatoire ...

Hướng dẫn php weekday index

Hướng dẫn import excel php

Đôi lúc chúng ta sẽ cần phải truy – xuất dữ liệu bằng file Excel như: xuất dữ liệu thống kê ra cho người dùng, hoặc import nhiều dữ liệu từ file excel vào ...

Hướng dẫn php weekday index

What does file () do in php?

File handling is an important part of any web application. You often need to open and process a file for different tasks.PHP Manipulating FilesPHP has several functions for creating, reading, ...

Hướng dẫn php weekday index

Hướng dẫn php regex non greedy

I have a string (from a file):ILX (New for 2013!) Overview: The least expensive route to Hondas premium-label goodness Drivetrain: Two four-cylinder engines to choose from as well as a ...