39

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

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

stormist
  • 5,709
  • 12
  • 46
  • 65

4 Answers4

84

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")));
Adnan
  • 25,882
  • 18
  • 81
  • 110
  • 2
    Other than the comma being in the wrong place in the format string, this looks like the best solution (right now, it'd show "October, 1 2009" instead of "October 1, 2009"). – Powerlord Apr 12 '10 at 21:20
  • How would you feed in a custom date to this. i.e. instead of using today, choose Feb 2, 2010 – stormist Apr 12 '10 at 21:38
  • 1
    @stormist, then use; date("F, 1 Y", strtotime("-6 months", strtotime("Feb 2, 2010"))); – Adnan Apr 12 '10 at 22:01
  • 2
    @Adnan, don't forget that you could merge those two calls to `strtotime` – salathe Apr 12 '10 at 22:26
  • 1
    @salathe, excellent point, I can use date("F, 1 Y", strtotime("-6 months Feb 2, 2010")); thank you. – Adnan Apr 12 '10 at 22:31
  • 1
    this still has the taste of slowness with it. parsing strings for dates and stuff. it works and is short, that's for sure – knittl Apr 12 '10 at 22:39
  • Caution! This will not always return what you expect, e.g. `date('M d, Y', strtotime("-1 months", strtotime("Dec 31, 2010"))) === 'Dec 01, 2010'`. – MaximeW Jan 31 '18 at 14:02
  • Its not full-proof, 1. echo date("F, 1 Y", strtotime("-6 months", strtotime("2019-10-30"))); => April, 1 2019 2. echo date("F, 1 Y", strtotime("-6 months", strtotime("2019-10-31"))); => May, 1 2019 Answer by @knittl works perfectly, https://stackoverflow.com/a/2625498/6080988 – Harikrishna Oct 31 '19 at 01:50
11

A bit hackish but works:

<?php

$date = new DateTime("-6 months");
$date->modify("-" . ($date->format('j')-1) . " days");
echo $date->format('j, F Y');

?>
Eric G
  • 4,018
  • 4
  • 20
  • 23
9

use a combination of mktime and date:

$date_half_a_year_ago = mktime(0, 0, 0, date('n')-6, 1, date('y'))

to make the new date relative to a given date and not today, call date with a second parameter

$given_timestamp = getSomeDate();
$date_half_a_year_ago = mktime(0, 0, 0, date('n', $given_timestamp)-6, 1, date('y', $given_timestamp))

to output it formatted, simply use date again:

echo date('F j, Y', $date_half_a_year_ago);
knittl
  • 246,190
  • 53
  • 318
  • 364
  • I don't have PHP here to test this, but does mktime even accept negative months? It's documented as returning false for invalid arguments. – Powerlord Apr 12 '10 at 21:22
  • @omg unicorns, yes, from the docs: »mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input.« – knittl Apr 12 '10 at 21:28
3

It was discussed in comments but the accepted answer has some unneeded strtotime() calls. Can be simplified to:

date("F 1, Y", strtotime("Feb 2, 2010 - 6 months"));

Also, you can use DateTime() like this which I think is equally as readable:

(new DateTime('Feb 2, 2010'))->modify('-6 months')->format('M 1, Y');

Or using static method....

DateTime::createFromFormat('M j, Y','Feb 2, 2010')
    ->modify('-6 months')
    ->format('M 1, Y');