Using ‘strtotime’ function to convert a custom-meta-box to a date-stamp

You should save the date in a format that is unambiguous so you will get predictable results. The particular format you prefer can’t be parsed by strtotime because it’s not clear which is the month, day, and year. If you convert the date when the meta data is saved, the contents of the field will change to that format and upon second save will become unparseable. It’s better to save it in a clear format and convert it when it’s output to your template.

You can simplify things for your client and be sure that the format will always be parseable by adding a date picker to the meta field that enforces a particular format. This script makes it very easy. Then, in your template:

$your_meta_field_contents="January 23 2012";
$time = strtotime( $your_meta_field_contents );
echo date( 'm d y', $time );

Leave a Comment