Use alternative titles for image - php

I have following code where inside image tag I open php tag and decide different images based on ifelse() condition. I want to add different titles for the images selected but could not come up with a solution. The code is as follows:
<img title = "Last assessment for this child was submitted <?php if ($time == 0){echo $time;}else{echo $time - 1;}?> Month(s) ago."
src="<?php
if ($record->$period == 0) { echo base_url()."img/warning.png";}
else{
date("M d, Y", strtotime($record->$period));
$vtime = new DateTime($record->$period); ///////////////////////
$today = new DateTime(); // for testing purposes ///Calculate Time period//
$diff = $today->diff($vtime); ///
$time = $diff -> m;
if($time <= 4)
{echo base_url()."img/green.png";}
elseif( $time > 4 && $time <= 6)
{echo base_url()."img/yellow.png";}
elseif($time >= 6)
{echo base_url()."img/red.png";}
}
"
/>
I want different title for the first condition. i.e. If the first condition is true and the image shown is "warning.png". Then the image title should be "Check record" instead of title "last assessment submitted was ...."
Any help is much appreciated.

You can simply use the following. Just nest current if...else condition in another if...else condition on title tag also.
<img title = "
<?php if($record->period==0)
echo "Check record";
else { ?>
Last assessment for this child was submitted <?php if ($time == 0){echo $time;}else{echo $time - 1;}?> Month(s) ago.
<?php } ?>"
src="<?php
if ($record->$period == 0) { echo base_url()."img/warning.png";}
else{
date("M d, Y", strtotime($record->$period));
$vtime = new DateTime($record->$period); ///////////////////////
$today = new DateTime(); // for testing purposes ///Calculate Time period//
$diff = $today->diff($vtime); ///
$time = $diff -> m;
if($time <= 4)
{echo base_url()."img/green.png";}
elseif( $time > 4 && $time <= 6)
{echo base_url()."img/yellow.png";}
elseif($time >= 6)
{echo base_url()."img/red.png";}
}
"
/>

Just like I've said in the comments, you could just separate the logic, make your calculations here and there. After you are done with it, set the variables and then echo it out in the presentation:
<?php
// initialization
$title = '';
$src = '';
// logic
$time = ($time == 0) ? $time : $time - 1;
$title = "Last assessment for this child was submitted %s Month(s) ago."; // initial
if ($record->$period == 0) {
$src = base_url() . "img/warning.png";
// override $title
$title = 'Check record';
} else {
$vtime = new DateTime($record->$period);
$today = new DateTime();
$diff = $today->diff($vtime);
$time = $diff->m;
if($time <= 4) {echo ;
$src = base_url()."img/green.png";
} elseif( $time > 4 && $time <= 6) {
$src = base_url()."img/yellow.png";
} elseif($time >= 6) {
$src = base_url()."img/red.png";
} else {
// whatever you need to do
}
}
$title = sprintf($title, $time);
?>
<!-- HTML MARKUP -->
<img title="<?php echo $title; ?>" src="<?php echo $src; ?>" />

You should avoid putting so much PHP code inline in the HTML to keep your code readable.
if ($record->period === 0) {
echo '<img src="img/warning.png" title="Warning title" />';
} else {
// Are you sure this does what you want?
// You probably need $record->period. (no $)
$vtime = new DateTime($record->$period);
$today = new DateTime();
$diff = $today->diff($vtime);
$time = $diff->m;
$title = 'Last assessment for this child was submitted ' .
($time === 0 ? 0 : $time-1) .
' month(s) ago.';
if ($time <= 4) {
echo '<img src="img/green.png" title="'. $title . '" />';
} else if ($time <= 6) {
// Don't need to check if it's bigger than 4, you've already checked this
// in the initial "if" and if that was succesful, we wouldn't be here.
echo '<img src="img/yellow.png" title="'. $title . '" />';
} else {
echo '<img src="img/red.png" title="' . $title . '" />';
}
}

PHP is easily embed with the HTML. So use it.
$imageURL = "";
if ($record->$period == 0) {
$imageURL = base_url() . "img/warning.png";
} else {
// date("M d, Y", strtotime($record->$period)); // This is not usable remove this statement
$vtime = new DateTime($record->$period); ///////////////////////
$today = new DateTime(); // for testing purposes ///Calculate Time period//
$diff = $today->diff($vtime); ///
$month = $diff->m;
if ($month <= 4) {
$imageURL = base_url() . "img/green.png";
} elseif ($month > 4 && $month <= 6) {
$imageURL = base_url() . "img/yellow.png";
} else {
$imageURL = base_url() . "img/red.png";
}
}
Rewrite your image tag as follow:
<img title = "Last assessment for this child was submitted <?php echo ($time)? $time - 1: $time; ?> Month(s) ago."
src="<?php echo $imageURL; ?>" />
There are some mistakes with your code.Kindly check the below points for further developments.
date("M d, Y", strtotime($record->$period)) : Why this statement is there as you have not saved the result saved by the date function.
$vtime : What is vtime ? Variable should I short and meaningful name.
$diff->m : This will return the month number so to be more specific instead of $time use $month as variable name to store the month value.
Regarding the if condition
First If : checking for $month is <= 4 : correct
Second If: checking for $month > 4 or <=6
Third elseif : in your old code. Why we need this because if it is not
satisfied by above two conditions then that means it is compulsory >= 6
then put this in else part directly.

Related

How to echo part of php from another file?

I have a WordPress page that is build out of different PHP files. In one the files there is a PHP Script that calculates the deadline when the application closes. I would like to echo this at the bottom of my page, but that's part of a different file.
What am i doing wrong?
File 1:
<?php $deadline = {
$now = time();
$dueDate = get_field('due_date');
$dueDate = strtotime($dueDate);
$datediff = $dueDate - $now;
$daysLeft = floor($datediff/(60*60*24))+1;
$daysLeft = intval($daysLeft);
?>
<?php if($daysLeft == 0) {
echo 'Last day';
} elseif($daysLeft < 0) {
echo 'Deadline passed';
} elseif($daysLeft == 1) {
echo $daysLeft.' days left';
} else {
echo $daysLeft.' days left';
}
}?>
File 2:
<?php
include('/../content-challenge.php');
echo $deadline;
?>
Note: as you see the file where file 2 points at is in a dir above the file it needs to be echoed in.
File 1.
<?php
function deadline() {
$now = time();
$dueDate = get_field( 'due_date' );
$dueDate = strtotime( $dueDate );
$datediff = $dueDate - $now;
$daysLeft = floor( $datediff / ( 60*60*24 ) )+1;
$daysLeft = intval( $daysLeft );
if( $daysLeft == 0 ) {
echo 'Last day';
}elseif( $daysLeft < 0 ) {
echo 'Deadline passed';
}elseif( $daysLeft == 1 ) {
echo $daysLeft.' days left';
}else {
echo $daysLeft.' days left';
}
}
?>
File 2.
If the function is defined in functions.php
<?php deadline(); ?>
or if is defined in any other file then you must include this file like you did (be sure that path is good)
<?php
include('/../content-challenge.php');
deadline();
?>

Conditional tag with DateInterval

how to use conditional tags using DateInterval with hour for scale in PHP
for example
i will add text "active" if at 21:00 until 23:00
<?php
$Hr = date('H:i');
if($hr == 21:00 - 23:00) {
echo "active";
}
else {
echo "not active";
}
Try this, use DateTime to compare time.
$time1 = new DateTime('21:00');
$time2 = new DateTime('23:00');
$interval = $time1->diff($time2);
//echo $interval;
$diff = $interval->format('%H').":".$interval->format('%I');
$Hr = date('H:i');
echo $diff." == ".$Hr."\n";
if($hr == $diff) {
echo "active";
}
else {
echo "not active";
}
DEMO
$Hr = date('H:i');
if(strtotime($hr)>strtotime(21:00) && strtotime($hr)<strtotime(23:00) ){
echo "active";
}else{
echo "not active";
}
Please try this this may work.

Check if date is within range in PHP strtotime

I have searched through SO but the answers that I've tried doesn't seem to solve my problem.
I have this simple code snippet where the user will input a numeric date, and a month, and the app will return the corresponding Zodiac Sign.
$birthdate = $_POST["birthdate"];
$birthmonth = (ucwords(strtolower($_POST["month"])))
//validations here. . .
$tmp = $birthmonth . " " . $birthdate;
$tmp2 = date_create_from_format('M j', $tmp);
$formatted_dob = date_format($tmp2, 'm-d-Y');
$dob = strtotime($formatted_dob);
echo $formatted_dob;
if ($dob >= strtotime('01-20-2016') && $dob <= strtotime('02-18-2016')) {
echo "Aquarius";
} elseif ($dob >= strtotime('02-19-2016') && $dob <= strtotime('03-20-2016')){
echo "Pisces";
}
Those echo stuff outside the if-else block are working fine, however if I input a value of 25 and February (which later on results to 02-25-2016), it always output Aquarius. How do you compare two strtotimevalues?
I've tried using DateTime object but it only gives me an error, which is another story. Thanks in advance.
Edited:
Change the order of your date (*your format on your date 01-20-2016 m-d-Y that's why when you convert it it becomes 1970-01-01 'Y-m-d' but if you change it into 2016-01-20 'Y-m-d' on your date range the code will work just fine in else-if.
$birthdate = $_POST["birthdate"];
$birthmonth = (ucwords(strtolower($_POST["month"])))
//validations here. . .
$tmp = $birthmonth . " " . $birthdate;
$tmp2 = date_create_from_format('M j', $tmp);
$formatted_dob = date_format($tmp2, 'm-d-Y');
$dob = strtotime($formatted_dob);
echo $formatted_dob;
$dobcompare = date_create(date('m/d/Y', $dob));
$aqstartdate = date_create(date('m/d/Y', strtotime('2016-01-20')));
$aqenddate = date_create(date('m/d/Y', strtotime('2016-02-18')));
$pistartdate = date_create(date('m/d/Y', strtotime('2016-02-19')));
$pienddate = date_create(date('m/d/Y', strtotime('2016-03-20')));
if ($dobcompare >= $aqstartdate && $dobcompare <= $aqenddate) {
echo "Aquarius";
}
elseif ($dobcompare >= $pistartdate && $dobcompare <= $pienddate) {
echo "Pisces";
} else {
echo "IDK";
}
Modify it in your need.
This is the example enter link description here

Wrong output on date

if(isset($_POST['submit_event'])){
$m = $_POST['event_month'];
$y = $_POST['event_year'];
$d = $_POST['event_day'];
$date = date('Y-n-d',strtotime($y. '-' .$m. '-' .$d));
echo $date;
//i always get 2013-10-07
}
All my inputted datas are correct although the output is always wrong and the same.
if (isset($_POST['submit_event']) && isset($_POST['event_month']) && isset($_POST['event_year']) && isset($_POST['event_day'])) {
$m = $_POST['event_month'];
$y = $_POST['event_year'];
$d = $_POST['event_day'];
$date_pre = $y. '-' .$m. '-' .$d;
$time = strtotime($date_pre)
$date = date('Y-n-d', $time);
echo $date;
}
// For debugging:
else {
echo "Not all variables have been set."
}

Is there Zend or jQuery libraries that can do this basic calendar function for me?

I have a calendar application, and in that calendar application, there is a "mini view" of a calendar. That little widget only displays the days of the currently chosen month and when you click the number it opens a new page and sends GET data to that new page (to display MySQL info, etc.)
The point is: this little mini-calendar doesn't do much at all, and I'm working to turn it into a partial in Zend Framework MVC. We have jQuery as well. I'm wondering if there is any built-in code that will easily do what we are trying to do with our own code.
Our code (done procedurally):
<?php
/***
This script file is the left panel calendar (small)
*/
//Required variables initializion starts (important in the case of create new event, to avoid PHP notices).
$day = "";
$month = "";
$year = "";
$sel = "";
$what = "";
$page = "index.php";
$param = "";
$index = "";
$functionLast = "goLastMonth";
$functionNext = "goNextMonth";
$sendFunction = "sendToForm";
if(isset($_GET['index'])) //if index page
{
$index = $_GET['index'];
}
if(isset($_GET['type'])) //if sype is set
{
$param = "&type=".$_GET['type'];
}
if(isset($_GET['page'])) //if page is set
{
$page = "calendar.php";
$param = '&page=calendar';
$functionLast = "getLastMonth";
$functionNext = "getNextMonth";
$sendFunction = "sendToTextBox";
}
if(!isset($calWidth) && !isset($calHeight)) //cal width /height check
{
$calWidth = CALENDAR_WIDTH;
$calHeight = CALENDAR_HEIGHT;
}
if(isset($_GET["day"])) //if day is set
$day = $_GET["day"]; //get it
if(isset($_GET["month"])) //if month is set
$month = $_GET["month"]; //..
if(isset($_GET["year"])) //..
$year = $_GET["year"]; //
if(isset($_GET["sel"]))
$sel = $_GET["sel"];
if(isset($_GET["what"]))
$what = $_GET["what"];
if(isset($_GET['date']))
{
$date = $_GET['date'];
list($year,$month,$day) = explode("-",$date); //split date into pieces
}
if($day == "") $day = date("j"); //if day is blank, get today
if($month == "") $month = date("m"); //if month is blank, get this month
if($year == "") $year = date("Y"); //if year is blank, get this year
//echo $day."-".$month."-".$year;die;
//echo '<br>';
if(!checkdate($month, $day, $year)) { //if not a valida date
if(isset($_GET["month"])) { //try to get number of days for this month as this seems the last day of the month. for example if today is 31 of August and you are calling ?month=9&year=2009 it gives you wrong results
$day = date("t", strtotime($year . "-" . $month . "-01")); //so give you 30.
}
}
$printabledate = $year."-".$month."-".$day;
$currentTimeStamp = strtotime("$year-$month-$day");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;
?>
<br />
<div id="loading1" class="a_loading1">
<iframe src="<?php echo SITE_URL?>/loading-msg.php" scrolling="no" frameborder="0" class="markup a_position"></iframe>
</div>
<table class="mini-cal-table">
<tr class="tprowbgcolor">
<td class="arrow" colspan='1' align="center"><input type='button' class='buttonleft' onclick='<?php echo "$functionLast($month,$year,\"$page\",\"$index\")"; ?>' onmousedown="this.className='maincalbutton_active_left'" onmouseout="this.className='buttonleft'" /></td>
<td class="title" colspan='5'><span class='title'><?php echo $monthName . " " . $year; ?></span></td>
<td class="arrow" colspan='1' align="center"><input type='button' class='buttonright' onclick='<?php echo "$functionNext($month,$year,\"$page\",\"$index\")"; ?>' onmousedown="this.className='maincalbutton_active_right'" onmouseout="this.className='buttonright'" /></td>
</tr>
<tr>
<td class='wd-titles'>Su</td>
<td class='wd-titles'>Mo</td>
<td class='wd-titles'>Tu</td>
<td class='wd-titles'>We</td>
<td class='wd-titles'>Th</td>
<td class='wd-titles'>Fr</td>
<td class='wd-titles'>Sa</td>
</tr>
<tr>
<?php
for($i = 1; $i < $numDays+1; $i++, $counter++)
{
$timeStamp = strtotime("$year-$month-$i");
if($i == 1)
{
// Workout when the first day of the month is
$firstDay = date("w", $timeStamp);
for($j = 0; $j < $firstDay; $j++, $counter++)
echo "<td> </td>";
}
if($counter % 7 == 0) {
echo "</tr><tr>";
}
if(date("w", $timeStamp) == 0) {
//$class = "class='weekend'";
$tdclass = "weekend";
} else {
if($i == date("d") && $month == date("m") && $year == date("Y")) {
//$class = "class='today'";
$tdclass = "today";
}
else {
//$class = "class='normal'";
$tdclass = "normal";
}
}
$zero = "";
if($i < 10 )
{
$zero = "0";
}
$month = round($month);
if($month < 10)
{
$month = "0".$month;
}
$date = $year."-".$month."-".$zero.$i;
?>
<td class="<?php echo $tdclass?>"><?php
if(!isset($_GET['page'])) {
?><a href='<?php echo SITE_URL; ?>/agenda.php?date=<?php echo $year; ?>-<?php echo $month; ?>-<?php echo $zero.$i; ?>'><?php echo $i?></a>
<?php } else {
?>
<a onclick='<?php echo "$sendFunction($i,\"$date\",$numDays,\"$index\",\"$type\")"; ?>'><?php echo $i?></a>
<?php
}
?></td>
<?php
}
?>
</tr>
</table>
<script language="javascript" type="text/javascript">
//<![CDATA[
function goLastMonth(month,year,page,index) {
// If the month is January, decrement the year.
if(month == 1) {
--year;
month = 13;
}
var url =
document.location.href = page+"?month="+(month-1)+"&year="+year+"<?php echo $param?>";
}
function goNextMonth(month,year,page,index)
{
// If the month is December, increment the year.
if(month == 12)
{
++year;
month = 0;
}
document.location.href = page+"?month="+(month+1)+"&year="+year+"<?php echo $param?>";
}
//]]>
</script>
jQuery has many good calendar options, the following being a part of the UI core:
http://jqueryui.com/demos/datepicker/
Also Zendx supports the jqueryui datepicker. Example

Categories