omg omg - 4 months ago 9
PHP Question

Manipulating Dates in PHP without using Regular Expressions

Is there a way to change a date from

1985-12-15


to

1985-12


without using a regular Expression?

Answer
<?php echo date('Y-m', strtotime('1985-12-15')); ?>

That should do it.

This will, using strtotime, convert 1985-12-15 to a unix timestamp. The date function then takes a second parameter timestamp on which to format the date.

Comments