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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

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
Beverley PortlockCommented:
"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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.