?
Solved

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

Posted on 2010-11-24
5
Medium Priority
?
1,049 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 143

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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

766 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