How can PHP be used to calculate the day of the week and calendar week from a given date?
To calculate the day of the week and calendar week from a given date in PHP, you can use the `DateTime` class along with the `format` method to extract the day of the week and `W` format character to get the calendar week.
$date = '2022-01-15';
$dateTime = new DateTime($date);
$dayOfWeek = $dateTime->format('l');
$calendarWeek = $dateTime->format('W');
echo "Day of the week: $dayOfWeek\n";
echo "Calendar week: $calendarWeek\n";
Keywords
Related Questions
- What is the purpose of {AliasNbPages} in FPDF and how does it help in generating page numbers in a PDF document?
- How can PHP variables be properly passed and used in SQL queries to avoid errors and ensure data retrieval?
- What are the potential security risks of passing form data from one PHP script to another on different servers?