Hello everyone, back again in pokaone. On this occasion, we will learn how to
get the day name, day number, and month name of a certain date. Intrigued?,
let's follow the tutorial below.
Previously I made a tutorial on how to retrieve all dates between two dates, click the following link to read. https://www.porkaone.com/2023/01/how-to-get-all-dates-between-two-dates.html
Now we will find out, day name, day number or month name of a date. This function is perfect for scheduling applications, calculating workdays or calculating holidays. You can improvise the example below with an article that I wrote before.
📆 How to Find Out the Name of the Day From the Date in PHP
// use timestamp
$timestamp = strtotime("2022-05-15");
echo date("l", $timestamp); // Output: "Tuesday"
// use datetime object
$date = new DateTime("2022-05-10");
echo $date->format("l"); // Output: "Tuesday"
📆 How to Find Out Indonesian Day Names from Dates in PHP
// first, you have to define the day names in your country
$days = array(
'Sun' => 'Minggu',
'Mon' => 'Senin',
'Tue' => 'Selasa',
'Wed' => 'Rabu',
'Thu' => 'Kamis',
'Fri' => 'Jumat',
'Sat' => 'Sabtu'
);
// use timestamp
$timestamp = strtotime("2022-05-10");
$day = date("D", $timestamp);
echo $days[$day]; // Output: "Selasa"
// use DateTime Object
$date = new DateTime("2022-05-10");
$day = $date->format("D");
echo $days[$day]; // Output: "Selasa"
📆 How to Find Out the Day Number From the Date in PHP
// use timestamp
$timestamp = strtotime("2022-05-10");
echo date("N", $timestamp); // Output: "2"
// use DateTime object
$date = new DateTime("2022-05-10");
echo $date->format("N"); // Output: "2"
📆 How to Find Out the Name of the Month From the Date in PHP
// use DateTime Object
$date = new DateTime("2022-05-10");
echo $date->format("F"); // Output: "May"
📆 How to Find Out the Month Names of a Certain Country by Date in PHP
// first, you have to define the month names in your country
$months = array(
'Jan' => 'Januari',
'Feb' => 'Februari',
'Mar' => 'Maret',
'Apr' => 'April',
'May' => 'Mei',
'Jun' => 'Juni',
'Jul' => 'Juli',
'Aug' => 'Agustus',
'Sep' => 'September',
'Oct' => 'Oktober',
'Nov' => 'November',
'Dec' => 'Desember'
);
// use timestamp
$timestamp = strtotime("2022-05-10");
$month = date("M", $timestamp);
echo $months[$month]; // Output: "Mei"
// use DateTime Object
$date = new DateTime("2022-05-10");
$month = $date->format("M");
echo $months[$month]; // Output: "Mei"
Ok, that's all for this tutorial about how to find out the day name, day
number and month name from a date. Hopefully useful and can be implemented. If
you have questions, please ask directly in the comments column below. That is
all and thank you.
0 Comments
Come on ask us and let's discuss together
Emoji