Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the first and last days of a given month

Tags:

date

php

I wish to rewrite a mysql query which use month() and year() functions to display all the posts from a certain month which goes to my function as a 'Y-m-d' parameter format, but I don't know how can I get the last day of the given month date.

$query_date = '2010-02-04'; list($y, $m, $d) = explode('-', $query_date); $first_day = $y . '-' . $m . '-01'; 
like image 369
dole doug Avatar asked Sep 24 '11 21:09

dole doug


People also ask

How do I get the first day of my current month?

1. First day of current month: select DATEADD(mm, DATEDIFF(m,0,GETDATE()),0): in this we have taken out the difference between the months from 0 to current date and then add the difference in 0 this will return the first day of current month.

How can I get first and last day of current month in php?

php $dt = "2008-02-23"; echo 'First day : '. date("Y-m-01", strtotime($dt)). ' - Last day : '. date("Y-m-t", strtotime($dt)); ?>

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

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


2 Answers

You might want to look at the strtotime and date functions.

<?php  $query_date = '2010-02-04';  // First day of the month. echo date('Y-m-01', strtotime($query_date));  // Last day of the month. echo date('Y-m-t', strtotime($query_date)); 
like image 170
Francois Deschenes Avatar answered Sep 19 '22 03:09

Francois Deschenes


I know this question has a good answer with 't', but thought I would add another solution.

$first = date("Y-m-d", strtotime("first day of this month")); $last = date("Y-m-d", strtotime("last day of this month")); 
like image 27
Goldbug Avatar answered Sep 21 '22 03:09

Goldbug