Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get week days in php?

Tags:

php

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


2 Answers

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.
like image 76
Carlos Avatar answered Oct 17 '25 01:10

Carlos


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.

like image 29
waiwai933 Avatar answered Oct 17 '25 01:10

waiwai933