Solved

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

Posted on 2010-11-24
5
1,045 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

724 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