In PHP, it's straightforward to get the date and the day of the week, primarily using the date() and strtotime() functions.
The date() function formats dates and times. Common date formats include:
echo date("Y/m/d"); // Example output: 2022/10/01
echo date("Y-m-d H:i:s"); // Example output: 2022-10-01 10:20:30
To get the current day of the week, use:
echo date("l"); // Outputs full weekday name, e.g., Monday, Tuesday
The strtotime() function converts an English textual datetime description into a Unix timestamp, which is useful for further processing. For example:
$date = "2022-10-01"; // Set date
echo date("l", strtotime($date)); // Outputs the corresponding weekday, e.g., Saturday
You can get today's weekday directly using date():
$today = date("l"); // Get today's weekday
echo "Today is: " . $today;
Example output: Today is: Saturday
By parsing a specified date with strtotime() and then using date(), you can get the weekday:
$date = "2022-10-01"; // Specific date
$weekdays = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); // Weekday array
$weekday = $weekdays[date("w", strtotime($date))]; // Get weekday name
echo $date . " is: " . $weekday;
Example output: 2022-10-01 is: Saturday