Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the first and last date in a month using PHP?

Tags:

date

php

How can I find the first and last date in a month using PHP? For example, today is April 21, 2010; I want to find April 1, 2010 and April 30, 2010.

like image 855
Karthik Avatar asked Apr 21 '10 05:04

Karthik


People also ask

How can I get start date and end date in php?

php function days($date1, $date2) { $date1 = strtotime($date1); $date2 = strtotime($date2); return ($date2 - $date1) / (24 * 60 * 60); } $date1 = '20100820'; $date2 = '20100930'; echo days($date1, $date2); ?>

How do you get the last day of the month in php?

$date = strtotime ( $datestring ); // Last date of current month. $day = date ( "l" , $lastdate );

How can I get first and last date of current week in php?

$monday = date('d-m-Y',strtotime('last monday',strtotime('next monday',strtotime($date)))); You have to get next monday first then get the 'last monday' of next monday. So if the given date is monday it will return the same date not last week monday.

How can I get 30 day date in php?

php $next_due_date = date('y-m-d',strtotime('+30 days',strtotime('echo $userRow3["due_date"]'))) .


2 Answers

The easiest way is to use date, which lets you mix hard-coded values with ones extracted from a timestamp. If you don't give a timestamp, it assumes the current date and time.

// Current timestamp is assumed, so these find first and last day of THIS month $first_day_this_month = date('m-01-Y'); // hard-coded '01' for first day $last_day_this_month  = date('m-t-Y');  // With timestamp, this gets last day of April 2010 $last_day_april_2010 = date('m-t-Y', strtotime('April 21, 2010')); 

date() searches the string it's given, like 'm-t-Y', for specific symbols, and it replaces them with values from its timestamp. So we can use those symbols to extract the values and formatting that we want from the timestamp. In the examples above:

  • Y gives you the 4-digit year from the timestamp ('2010')
  • m gives you the numeric month from the timestamp, with a leading zero ('04')
  • t gives you the number of days in the timestamp's month ('30')

You can be creative with this. For example, to get the first and last second of a month:

$timestamp    = strtotime('February 2012'); $first_second = date('m-01-Y 00:00:00', $timestamp); $last_second  = date('m-t-Y 12:59:59', $timestamp); // A leap year! 

See http://php.net/manual/en/function.date.php for other symbols and more details.

like image 193
Nathan Long Avatar answered Oct 08 '22 09:10

Nathan Long


Simple one

  • Y - A full numeric representation of a year, 4 digits
  • m - Numeric representation of a month, with leading zeros
  • t - Number of days in the given month

Reference - http://www.php.net/manual/en/function.date.php

<?php     echo 'First Date    = ' . date('Y-m-01') . '<br />';     echo 'Last Date     = ' . date('Y-m-t')  . '<br />'; ?> 
like image 22
Siva Kranthi Kumar Avatar answered Oct 08 '22 07:10

Siva Kranthi Kumar