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?
RefaelAsked:
Who is Participating?
 
Beverley PortlockConnect With a Mentor Commented:
"Thanks!  So you suggest i better use YYYY-MM-DD"

Let me be clear. YYYY-MM-DD is the general form of a date. An example would be 2011-03-29 for 29th March 2011. The PHP date function produces a date in this format using this code

date("Y-m-d")

The Y indicates a 4 year date, the 'm' indicates a two digit month with an optional leading zero and the 'd' indicates a two digit day with an optional leading 0. The full functions for date are shown at http://www.php.net/date and if you scroll down a bit the format letters and their functions are explained as well with examples.
0
 
Beverley PortlockCommented:
strtotime will do it. It does most formats

<?php

$startDate = "March 19, 2011";

echo date("Y-m-d", strtotime( $startDate ) );

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
0
 
RefaelAuthor Commented:

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)?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
RefaelAuthor Commented:

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";}

?>

Open in new window

0
 
Beverley PortlockCommented:
Yes - that looks like it should work. The advantage of YYYY-MM-DD dates is that they can be compared using simple < and > operations
0
 
RefaelAuthor Commented:

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");

??

0
 
gwkgCommented:
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();
0
 
gwkgCommented:
The following will be true

time() > strtotime('January 10 2010');

time() < strtotime('January 10 2025');
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.