{"flag":true,"single":true,"pageTitle":"Function to get days between two dates php","post":{"id":120,"user_id":"1","slug":"function-to-get-days-between-two-dates-php-7dqg","title":"Function to get days between two dates php","body":"<p>The problem we want to solve is to calculate the number of days between two dates in PHP, regardless of the format of the date string.<\/p>\r\n<pre class=\"language-markup\"><code>function days_between($date1, $date2) {\r\n  \/\/ Remove the day of the week from the date string\r\n  $date1 = preg_replace('\/\\b\\w+\\b, \/', '', $date1);\r\n  $date2 = preg_replace('\/\\b\\w+\\b, \/', '', $date2);\r\n\r\n  \/\/ Create DateTime objects from the input dates\r\n  $datetime1 = date_create($date1);\r\n  $datetime2 = date_create($date2);\r\n\r\n  \/\/ Calculate the difference between the two dates\r\n  $interval = date_diff($datetime1, $datetime2);\r\n\r\n  \/\/ Return the number of days in the interval\r\n  return $interval-&gt;days;\r\n}<\/code><\/pre>\r\n<p><strong>Output:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>echo days_between(\"Friday, 14 April 2023\",\"Friday, 18 April 2023\"); \/\/ Outputs: 4\r\necho days_between(\"2023-04-12 09:15:29\", \"2023-04-14 09:15:29\"); \/\/ Outputs: 2\r\necho days_between(\"2023-04-12\", \"2023-04-15\"); \/\/ Outputs: 3<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>The <code>days_between()<\/code> function takes two dates as input parameters and returns the number of days between them. To handle different date formats, the function first removes the day of the week from each date string using the <code>preg_replace()<\/code> function. It then creates <code>DateTime<\/code> objects from the input dates using the <code>date_create()<\/code> function, which can parse a wider range of date formats.<\/p>\r\n<p>The function then calculates the difference between the two <code>DateTime<\/code> objects using the <code>date_diff()<\/code> function, which returns a <code>DateInterval<\/code> object. Finally, it extracts the number of days from the <code>DateInterval<\/code> object using the <code>days<\/code> property and returns it as the result of the function.<\/p>\r\n<p>For example, if you call <code>days_between(\"Friday, 14 April 2023\",\"Friday, 18 April 2023\")<\/code>, the function will remove the day of the week from each date string, resulting in \"14 April 2023\" and \"18 April 2023\". It will then create <code>DateTime<\/code> objects from these dates, calculate the difference between them, and return the number of days as 4.<\/p>\r\n<p>I hope this explanation helps you understand how the code works!<\/p>\r\n<p>The <strong>days_between()<\/strong> function takes two dates as input parameters and returns the number of days between them. To handle different date formats, the function first&nbsp;removes the day of the week from each date string using the preg_replace() function. It then creates DateTime objects from the input dates using the date_create() function, which can parse a wider range of date formats.<\/p>\r\n<p>The function then calculates the difference between the two DateTime objects using the date_diff() function, which returns a DateInterval object. Finally, it extracts the number of days from the DateInterval object using the days property and returns it as the result of the function.<\/p>\r\n<p>For example, if you call days_between(\"Friday, 14 April 2023\",\"Friday, 18 April 2023\"), the function will remove the day of the week from each date string, resulting in \"14 April 2023\" and \"18 April 2023\". It will then create DateTime objects from these dates, calculate the difference between them, and return the number of days as 4.<\/p>\r\n<p>I hope this explanation helps you understand how the code works!<\/p>\r\n<p><strong>How to get today date:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>date(\"Y-m-d H:i:s\")<\/code><\/pre>\r\n<p>Sample<\/p>\r\n<pre class=\"language-markup\"><code>echo days_between($job-&gt;created_at,date(\"Y-m-d H:i:s\")) days Ago<\/code><\/pre>","category_id":"1","is_private":"0","created_at":"2023-04-14T06:09:01.000000Z","updated_at":"2023-04-14T06:09:01.000000Z","category":{"id":1,"user_id":"1","name":"PHP","slug":"php-3ius","parent_id":null,"created_at":"2023-03-14T03:58:19.000000Z","updated_at":"2023-03-14T03:58:19.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"The problem we want to solve is to calculate the number of days between two dates in PHP, regardless of the format of the date string. funct - Function to get days between two dates php (Updated: April 14, 2023) - Read more about Function to get days between two dates php at my programming site [SITE]","categories":[]}