Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: strtotime how to get last july? (or other specific month)

if now is 2016-02-10 I need for return 2015-07-01 (or 2015-07-10)

strtotime('july'); // returns 2016-07-10
strtotime('last july'); // does not work
strtotime('july 2015'); // I have not '2015' for query. 
//Only if compare date (month) now and month needed. In looks not realy straight way.
like image 553
scrache Avatar asked Nov 01 '25 12:11

scrache


1 Answers

What happens if you are past july this year? Would it then need to return july 2016, or is it always the year before?

If so, you can use 'july last year':

<?php
$returnValue = strtotime('july last year');

Based on needing to return this year's july as well:

<?php
$returnValue = strtotime('july '. (date("n") >= 7 ? 'this' : 'last' ) .' year');
like image 111
Liam Wiltshire Avatar answered Nov 04 '25 02:11

Liam Wiltshire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!