Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: simplest way to get the date of the month 6 months prior on the first?

So if today was April 12, 2010 it should return October 1, 2009

Some possible solutions I've googled seem overly complex, any suggestions?

like image 377
stormist Avatar asked Apr 12 '10 21:04

stormist


People also ask

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

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!

How can I get last date of previous month in php?

Get Last Day Of The Previous Month:$lastDay = date('t',strtotime('last month')); print_r($lastDay);

How can I get previous date from datetime in php?

php $m = date("m"); // Month value $de = date("d"); // Today's date $y = date("Y"); // Year value echo "Yesterday's date was: " . date('d-m-Y', mktime(0,0,0,$m,($de-1),$y)); ?>

How do you check if a date is before another date in php?

if (strtotime($date) > mktime(0,0,0)) should do the job.


2 Answers

Hm, maybe something like this;

echo date("F 1, Y", strtotime("-6 months")); 

EDIT;

if you would like to specify a custom date use;

echo date("F, 1 Y", strtotime("-6 months", strtotime("Feb 2, 2010"))); 
like image 67
Adnan Avatar answered Sep 30 '22 08:09

Adnan


A bit hackish but works:

<?php  $date = new DateTime("-6 months"); $date->modify("-" . ($date->format('j')-1) . " days"); echo $date->format('j, F Y');  ?> 
like image 22
Eric G Avatar answered Sep 30 '22 08:09

Eric G