Solved

How to get current month starting and ending date in php?

Posted on 2010-11-24
5
1,038 Views
Last Modified: 2012-06-21
I want the current month's start, end date using PHP. what is the easiest method to find out?
0
Comment
Question by:Loganathan Natarajan
  • 2
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34203798
you can use mktime() to do so:
http://www.php.net/manual/en/function.mktime.php
http://php.net/manual/en/function.date.php
$month = date("n");
$year = date("Y");
$this_month_start = mktime(0,0,0, $month, 1, $year);
$next_month_start = mktime(0,0,0, $month + 1, 1, $year);

Open in new window


note, you will use some condition like:
yourvalue >= $this_month_start AND yourvalue < $next_month_start

hope this helps

0
 
LVL 17

Expert Comment

by:sweetfa2
ID: 34203848
$start = date('dmY');
$start{0} = '0';
$start{1} = '1';
$endmonth = date('tmY');
0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 34203852
how can i format this as YYYY-mm-dd

actually i need to take  month starting and end date to get the 1 month results dynamically..

for example this month,

starting date = 2010-11-01
end date = 2010-11-30
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 34203885
you use date() function again:

$month = date("n");
$year = date("Y");
$this_month_start = mktime(0,0,0, $month, 1, $year);
$next_month_start = mktime(0,0,0, $month + 1, 1, $year);
print date("Y-n-d", $this_month_start);
print date("Y-n-d", $next_month_start);

Open in new window

0
 
LVL 36

Author Closing Comment

by:Loganathan Natarajan
ID: 34204133
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now