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.
<?php $datetime = DateTime::createFromFormat('d/m/Y', '05/06/2021'); echo $datetime->format('l'); // echo $datetime->format('D'); // Output: Sat
'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.
<?php // Date Format: Y/m/dd $paymentDate = '2021/06/05';; echo $day = date('l', strtotime($paymentDate)); // echo $day = date('D', strtotime($paymentDate)); // Output: Sat die();
Example 3: Extract Day Name Using date_format() Function
We can also use the date_format() function to grab the day name in PHP.
<?php // Date Format: Y/m/dd $date = date_create("2013-03-15"); echo date_format($date,"l"); // echo date_format($date,"D"); // Output: Fri die();
Additionally, read our guide:
- Base Table Or View Already Exists In Laravel Migration
- Add Column After A Column In Laravel Migration
- Laravel: Change Column Type In Migration
- Laravel: Change Column Name In Migration
- How To Use Where Date Between In Laravel
- Laravel: Get Year From Date
- Laravel Remove Column From Table In Migration
- Difference Between Factory And Seeders In Laravel
- Difference Of require, include, require_once And include_once
- How To Calculate Age From Birthdate
- How To Check Laravel PHP Version
- How To Handle Failed Jobs In Laravel
- How To Remove WooCommerce Data After Uninstall
- How To Get Latest Records In Laravel
- How To Break Nested Loops In PHP Or Laravel
- How To Pass Laravel URL Parameter
- 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!