How to calculate the number of days between two dates in PHP?
Hi everyone! I'm a beginner in PHP and I can't figure out dates. I need to write a script that calculates the number of days between two dates. For example, I want to know how many days have passed since the beginning of the year until today, or how many days are left until New Year.\n\nI tried something like this:\n\n```php\n$date1 = '2023-01-01';\n$date2 = '2023-12-31';\n$days = ($date2 - $date1) / (60 * 60 * 24);\necho $days;\n```\n\nBut it doesn't work — it outputs some nonsense or an error. I understand that strings can't be subtracted like that, but what's the correct way? There are many different tips online: some recommend `strtotime()`, some recommend `DateTime`, and others recommend `date_diff`. I'm completely confused.\n\nPlease tell me the most correct and modern way to find the difference in days between two dates in PHP. It's important that the solution
No answers yet.
Log in to leave an answer
Blogs
Clean Code in Java: Clean Code Principles with Examples
SOLID Principles in TypeScript: Theory and Practice with Examples
GraphQL API with Java: Step-by-Step Tutorial for Beginners
Hexagonal Architecture in Java: Clean Logic Without Pain