Php get day name from date

$date = new \DateTime("now", new \DateTimeZone('Asia/Calcutta') );
         $day = $date->format('D');
         $weekendnaame = weekedName();
         $weekid =$weekendnaame[$day];
          $dayname = 0;
        $weekiarray = weekendArray($weekid);
        foreach ($weekiarray as $key => $value) {
            if (in_array($value, $request->get('week_id')))
          {
            $dayname = $key+1;
            break;
          }
        }


weeknDate($dayname),

function weeked(){
  $week = array("1"=>"Sunday", "2"=>"Monday", "3"=>"Tuesday", "4"=>"Wednesday", "5"=>"Thursday", "6"=>"Friday", "7"=>"Saturday");
  return $week;
}
function weekendArray($day){
        $favcolor = $day;

switch ($favcolor) {
  case 1:
    $array = array(2,3,4,5,6,7,1);
    break;
  case 2:
    $array = array(3,4,5,6,7,1,2);
    break;
  case 3:
    $array = array(4,5,6,7,1,2,3);
    break;
    case 4:
    $array = array(5,6,7,1,2,3,4);
    break;
    case 5:
    $array = array(6,7,1,2,3,4,5);
    break;
    case 6:
    $array = array(7,1,2,3,4,5,6);
    break;
     case 7:
    $array = array(1,2,3,4,5,6,7);
    break;
  default:
    $array = array(1,2,3,4,5,6,7);
}
return  $array;
}
function weekedName(){
  $week = array("Sun"=>0,"Mun"=>1,"Tue"=>3,"Wed"=>4,"Thu"=>5,"Fri"=>6,"Sat"=>7);
  return $week;
}

Home » PHP: Get Day Name From Date

Last updated on June 5, 2021 by

In this tutorial, we will learn how to get the day name from the date in PHP. The PHP provides very useful functions and classes by using it we can easily format the date. Let’s just dive into it and explore it.

Get Day Name From Date In PHP

PHP provides the date() method which is used to format the date as we want. We can also get the day name from the date. Also, this function is useful to change any kind of date format. Let’s see simple examples.

Example 1: Extract Day Name From Static Date

In this example, we have used the createFromFormat() function which helps us to provide exact conversion from the specific date format else it might interpret the wrong date format and gives us the wrong output.

It is always advisable to use createFromFormat() function while dealing with the static dates or if you changed the default date format.

format('l');
    // echo $datetime->format('D');  // Output: Sat
    

Output:

Saturday

Notes: Use 'D' instead of 'l' to get the three letters day name for ex. Sat, Mon, Sun, etc.

Example 2: Extract Day Name Using date() Function

If you don’t want to use the DateTime class then you can simply use the PHP’s date() function to get the day name from date.

Output:

Saturday

Example 3: Extract Day Name Using date_format() Function

We can also use the date_format() function to grab the day name in PHP.

Output:

Friday

Additionally, read our guide:

  1. Base Table Or View Already Exists In Laravel Migration
  2. Add Column After A Column In Laravel Migration
  3. Laravel: Change Column Type In Migration
  4. Laravel: Change Column Name In Migration
  5. How To Use Where Date Between In Laravel
  6. Laravel: Get Year From Date
  7. Laravel Remove Column From Table In Migration
  8. Difference Between Factory And Seeders In Laravel
  9. Difference Of require, include, require_once And include_once
  10. How To Calculate Age From Birthdate
  11. How To Check Laravel PHP Version
  12. How To Handle Failed Jobs In Laravel
  13. How To Remove WooCommerce Data After Uninstall
  14. How To Get Latest Records In Laravel
  15. How To Break Nested Loops In PHP Or Laravel
  16. How To Pass Laravel URL Parameter
  17. Laravel Run Specific Migration

That’s it from our end. We hope this article helped you to get day name from the date in PHP.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

How can I get today's day in PHP?

You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

How do you get the day of the week from a date in PHP?

Program 1: Get the default first day of a week when the time-string is “this week”. $firstday = date ( 'l - d/m/Y' , strtotime ( "this week" ));

How get fetch date from database in PHP?

Create a Date With mktime() The optional timestamp parameter in the date() function specifies a timestamp. If omitted, the current date and time will be used (as in the examples above). The PHP mktime() function returns the Unix timestamp for a date.

How can I compare two dates in PHP?

we can analyze the dates by simple comparison operator if the given dates are in a similar format. php $date1 = "2018-11-24"; $date2 = "2019-03-26"; if ($date1 > $date2) echo "$date1 is latest than $date2"; else echo "$date1 is older than $date2"; ?>