Rocco The Taco
Rocco The Taco

Reputation: 3777

Insert dynamic DATE metadata with PHP

I am importing content using cron with a PHP script and has a field called _expiration_date that I need to dynamically populate 20 days in advance based on the current date of import. If I create the record manually using the PHP program and look at the record, the program creates the data in the _expiration_date field in this format: 1449203160 which translates out to human form as Dec 4, 2015 @ 4:26.

I assume this is some sort of date/timestamp?

As you can see below the fields I am populating are static text. How do I populate the _expiration_date field dynamically in the correct date/time format to be read by the system?

        }
        update_post_meta( $post_id, 'adverts_location', $parent->name );
        update_post_meta( $post_id, 'adverts_person', 'John Doe');
        update_post_meta( $post_id, 'adverts_email', '[email protected]');
        update_post_meta( $post_id, '_expiration_date', '');
    }

Upvotes: 0

Views: 90

Answers (2)

Lucarnosky
Lucarnosky

Reputation: 514

$convertedDate = DateTime::setTimestamp($post_id);

This will convert your timestamp to DateTime, now you can manipulate it as a normal DateTime

   $expirationDate = date_add($convertedDate , date_interval_create_from_date_string('20 days'));

Or you can try this if you have PHP 5 or greater

$convertedDate = date('Y-m-d','1449203160');
$convertedDate = new DateTime($convertedDate);
$convertedDate->add(new DateInterval('P20D'));
echo($convertedDate);

Upvotes: 1

JorisK
JorisK

Reputation: 86

It is a linux timestamp. To populate your expiration date you can use:

strtotime('+20 days');

Upvotes: 1

Related Questions