Refael
asked on
string to date via custom field WP 3
I am posting a date as a string via the custom fields to a post in wordpress 3.0.
So i simply input in the custom filed something like this:
March 19, 2011
i need to compare this string date to the current date to check if the custom field date is greater, less or equal to execute/display different title.
i am a newbie to php so i i can only write the IF statement but have no clue how to convert the string to a date format that the computer can understand and then to compare these two dates.
please help?
ASKER
Hi bportlock,
so i basically did this:
$startDate = $post_event_date;
echo date("Y-m-d", strtotime( $startDate ) );
how do i compare this date to the today's date (e.g. grater then, less or equal)?
ASKER
Hi, would this work?
<?php
$post_event_date = 'post_event_date';
$post_event_date = get_post_meta($post->ID, $post_event_date, TRUE);
$event_date = date("Y-m-d", strtotime( $post_event_date ));
echo $event_date;
echo "<br>";
$today_date = date('Y-m-d');
echo $today_date;
echo "<br>";
if ($today_date > $event_date) {echo "past events";}
if ($today_date < $event_date) {echo "upcoming events";}
?>
Yes - that looks like it should work. The advantage of YYYY-MM-DD dates is that they can be compared using simple < and > operations
ASKER
Thanks! So you suggest i better use YYYY-MM-DD
e.g.
$event_date = date("YYYY-MM-DD", strtotime( $post_event_date ));
$today_date = date("YYYY-MM-DD");
??
You can compare todays timestamp time() with a string date with strtotime()
http://php.net/manual/en/function.strtotime.php
Also, strtotime('now') is the same as time();
http://php.net/manual/en/function.strtotime.php
Also, strtotime('now') is the same as time();
The following will be true
time() > strtotime('January 10 2010');
time() < strtotime('January 10 2025');
time() > strtotime('January 10 2010');
time() < strtotime('January 10 2025');
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Open in new window
outputs 2011-03-19. See http://www.php.net/strtotime for more info and http://uk.php.net/manual/en/datetime.formats.php for the date and time formats supported