homepage Welcome to WebmasterWorld Guest from 107.22.37.143
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Help comparing conditions and returining multiple results
I wnat to evaluate the date in Wordpress and give multiple answers
calvinmicklefinger




msg:4396843
 2:52 am on Dec 12, 2011 (gmt 0)

Hello,

I am trying to insert a PHP snippet in a wordpress post. I am using the execphp to allow the running of php code.

I have the following code working and displaying. I would like to create a snippet that would allow me to echo multiple responses if multiple conditions are met. Any suggestions on how to this.

Please note, I am not an educated coder. I merely cobble together things that look like they may work.

Here's my code: (Wordpress uses "the_date()" to retrieve the date the post was made.)

<p>Publication was on <?php echo the_date('Y-m-d'); ?>

<? function dateDiff ($d1, $d2) {
// Return the number of days between the two dates:
return round(abs(strtotime($d1)-strtotime($d2))/86400);
} // end function dateDiff
?>

<?php
$date_01 = get_the_date('U');
$date1 = date('Y-m-d', $date_01);
$date2 = '2011-12-15';
echo (dateDiff($date1, $date2));
?>

<?php
if ((dateDiff($date1, $date2)) > 30)
echo '<p>If the post was over 30 days ago, they get this and the prior messages';
elseif ((dateDiff($date1, $date2)) > 20)
echo '<p>If the post was over 20 days ago, they get this and the prior message';
else
echo '<p>If the post was made under 20 days ago, they get this message only';
?>

 

Habtom




msg:4396883
 4:59 am on Dec 12, 2011 (gmt 0)

if ... else if .... else
This structure allows only one of them to be met at a time.

if ((dateDiff($date1, $date2)) > 30) {
echo '<p>If the post was over 30 days ago, they get this and the prior messages';
}

if ((dateDiff($date1, $date2)) > 20) {
echo '<p>If the post was over 20 days ago, they get this and the prior message';
}

The above code, for example, allows you to return two separate message if the two conditions are met. I hope you see where this is going.

rocknbil




msg:4397148
 6:10 pm on Dec 12, 2011 (gmt 0)

Umm . . slight mod. :-)

$msg = null;

if ((dateDiff($date1, $date2)) > 30) {
$msg .= ' some over 30 day message';
}

if ((dateDiff($date1, $date2)) > 20) {
$msg .= ' some over 20 day message';
}

if ($msg) { echo "<p>$msg</p>"; }

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved