Nirav Joshi Nirav Joshi - 3 years ago 101
PHP Question

date half month not working

I tried to do like this

date('Y-m-d', strtotime('0.5 months'));


But it gives me date 1970-01-01

Expected date is 2017-10-15

So the question is why the half month is not working?

Answer Source

You can't use non-integers in a strtotime() relative date format:

number [+-]?[0-9]+

To make this work, you can use either 2 weeks or 15 days (note that these two return different days, since 2 weeks is 14 days):

<?php
echo date("Y-m-d", strtotime("2 weeks")).PHP_EOL; // 2017-10-15
echo date("Y-m-d", strtotime("15 days")).PHP_EOL; // 2017-10-16
echo date("Y-m-d", strtotime("+.5 months")).PHP_EOL; // not valid, returns 1970-01-01

Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download