I wonder if anyone could help me with this.
I have two fields in a database that are shown using:
1-<?php echo JHTML::_('date', $row->created_date, 'd/m/Y'); ?>
2-<?php echo $row->delivery_date; ?>
The first one, is written to the database as a full date with time, and as you can see above, I strip the output to only show the date. In the database it would appear as '2013-09-10 11:56:52'
The second is from a text entry form field, that has just text saved to the database in the format d/m/Y. In the database this appears as '19/09/2013'
Is there a way I can produce an if statement, that will add a span and class tag around the 2nd line if this condition is true:
"If the $row->delivery_date is within 21 days after (and including)
$row->created_date."
Would it ultimately be best if I made them both full date values? Would that make it easier to calculate how many days apart they are?
Convert Dates to a common format using strtotime() (e.g. PHP works nicely with unix timestamp's):
$created_date = strtotime($row->created_date); // These are basically seconds
$deliver_date = strtotime($row->delivery_date);
Calculate the difference between the two and covert it to days (each day is 86400 seconds):
$days = ceil(abs($deliver_date - $created_date) / 86400);
Check $days in your if() statement for echoing your span.
Something like:
if($days <= 21)
{
echo '<span class="delivery_warning">' . $row->delivery_date . '</span>';
}
else
{
else { echo $row->delivery_date; };
}
Related
I'm trying to have make a simple script which hides content when the date hits.
So when it's 03-11-2016, show the content but when the date is 03-15-2016, hide the content.
if(date(m-d-Y) > "03-15-2016") {
// do stuff
}
Sadly the script below isn't working for some reason, any help would be nice, thanks!
date function returns a string. Comparing strings may cause some unexpected results. What you need to compare is timestamps of dates:
if (strtotime("now") > strtotime("03/15/2016 00:00:00")) {
// do stuff
}
Note, I changed your "03-15-2016" to "03/15/2016" cause it's format, which can be parsed by strtotime function.
If your wanting to maintain the same format as your current script, this will work:
<?php
if(strtotime(date("Y-m-d")) > strtotime("2016-1-04")) {
// do stuff
}
?>
think about using something like:
$today = time(); // time() is now (timestamp) since epoch
$hideIt = strtotime("2016-03-15"); // timestamp of date to hide stuff
if ( $today > $hideIt ) // if today is greater than the date we want to hide things
{
// do stuff...
}
Your syntax is not working because you are using parameters in a wrong order. You need to use them in order by coming first the year, month and the day:
if (date('Y-m-d') > '2016-03-15') {
// do stuff
}
this is my events script that pulls out appointments for the next 7 days, it appears to work ok, but only under one condition........The dates and times are held in the mysql db in datetime format so 2013-12-23 08:30:00 . My script prints out each day and finds appointments for that day for customers that are dropping off or picking up things. The mysql looks through the db and matches the customers with the dropping off or picking up fields to the date being printed and adds them in the div below the date.
The problem I am having is that if the time is set to anything other than 00:00:00 it doesn't pickup that customer for that day. How do I get the comparison to ignore the time and only use the date ?.
// Date box container
echo '<div class="dateboxcontainer">';
// Loop through and create a date for the next 7 days
$days = new DatePeriod(new DateTime, new DateInterval('P1D'), 7);
foreach ($days as $day) {
echo '<div class="datebox">';
echo '<div class="topdate">';
echo strtoupper($day->format('D d')) . PHP_EOL;
echo '</div>';
// Get the names for each day
$theday = strtoupper($day->format('Y-m-d'));
$sqldate = <<<SQL
SELECT *
FROM `jobdetails`
WHERE datedroppingoff = '$theday' OR datepickingup = '$theday'
SQL;
if(!$resultdate = $db->query($sqldate)){
die('There was an error running the query [' . $db->error . ']');
}
while($rowdate = $resultdate->fetch_assoc()){
echo $rowdate['name'];
}
//
echo '</div>';
}
echo '</div>';
//
What you are doing right now is comparing date/time values to just date values. This comparison would fail if the time part is anything other than midnight.
You can fix the comparison by using the DATE() MySql function to compare apples with apples:
WHERE DATE(datedroppingoff) = '$theday' OR DATE(datepickingup) = '$theday'
There are other ways to do the same, for example
WHERE DATEDIFF(datedroppingoff, '$theday') = 0 OR ...
If you had a $nextday value at hand you could also do
WHERE (datedroppingoff >= '$theday' AND datedroppingoff < '$nextday') OR ...
You are storing a specific time and day in mySQL, but only search for a date in your SQL query. As mySQL does not understand the difference between you wanting to search for a complete day or a specific point in time, mySQL assumes you are looking for the day at time 0:00:00.
You have a few options, you could search for a time period (pseudo code, check the borders yourself):
WHERE datedroppingoff > '$theday' AND datedroppingoff < '$theday'+1
another option is to store the date and time in separate db fields. That way you can keep your SQL queries simpler.
Good luck.
I want to write an IF statement based on two dates.
I have a MySQL database (of which I have no control over) for tests and exams taken.
One of the fields is labelled ClientTime and outputs the date and time the test was taken. When queried the typical output would read as such Thu Aug 25 16:47:05 GMT+0100 2011
The following all happens in a while loop
I’ve managed to convert this MySQL date using strtotime() to a d/m/Y format so the above would read 25/08/2011 and I can display a table with all the correct dates.
I also have a php form that asks for the number of days that this test will expire after and this gets stored in a variable $IMonths (from the $_POST['IMonths'] passed from the form page) - the php code will be changed to Months once I have it working but I am using days for the purpose of testing – hence the variable passed being called $IMonths.
I have also created a $today = date("d/m/Y"); variable
I can successfully create a new date using the following command $Expiry = date('d/m/Y', strtotime($row['ClientTime'] . "+$IMonths days")); and can display all results with both the test date and the expiry date.
I then want to include an if-statement within my while-statement that says is ($Expiry>$today) but it’s not accurate – there are no errors and changing the number of days changes the results.
My test database has 5 exams in with the following dates.
Taken 17/08/2011
Taken 17/08/2011
Taken 22/08/2011
Taken 22/08/2011
Taken 24/08/2011
If I set the expiry days to 1, I get all results back as expected:
If I set it to 10 days I get two results
Taken 17/08/2011
Taken 17/08/2011
I don’t get any of the others even though their date will be less than today's date (07/09/2011 (as I write this)) but they do expire in the month of September.
If I set the expiry days to 15, I get one result that is one day in the future!
Taken 17/08/2011
I hope that makes sense. My code is as follows:
<?php
// Make The MySQL Server Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
// Make The MySQL Database Connection
mysql_select_db("questions") or die(mysql_error());
$IQuizName = $_POST['IQuizName'];
$IMonths = $_POST['IMonths'];
$result = mysql_query("SELECT * FROM questions.resultsets
WHERE QuizName='$IQuizName'
AND PassFail='Pass'") or die (mysql_error());
$today = date("d/m/Y");
echo "<table border='1'>";
echo "<tr> <th align='left'>Candidate Name </th> <th align='left'>Pass / Fail</th> <th align='left'>Date last Taken</th> <th align='left'>Expires</th> <th align='left'>Today</th> </tr>";
while($row = mysql_fetch_array($result)){
//START of expiry calculation
$Expiry = date('d/m/Y', strtotime($row['ClientTime'] . "+$IMonths days"));
if($Expiry>$today){
echo "<tr><td>";
echo $row['Candidate'];
echo "</td><td>";
echo $row['PassFail'];
echo "</td><td>";
//Date Conversion
echo date('d/m/Y', strtotime($row['ClientTime']));
echo "</td><td>";
echo $Expiry;
echo "</td><td>";
echo $today;
echo "</td><tr>";
};
}
echo "</table>";
echo "<P>";
?>
Your issue is due to datatypes in PHP. When you call the date() function in PHP, you are actually formatting dates into strings. So when you do $Expiry = date(blahblah), $expiry is now a string. So your if statement is if (somestring > someotherstring). Normally you would not think to compare one block of text to see if it is greater than another block of text.
http://www.php.net/manual/en/function.date.php
One way to get around this would be the strtotime function. This returns integers that are based on UNIX timestamp for the date representation.
http://www.php.net/manual/en/function.strtotime.php
If you convert your string dates to strtotime, your if statement is now comparing if one number is greater than another. This should solve your problem :)
First: fix that SQL-injection hole.
I suggest querying the expiry dates in SQL, it will be much easier.
Here's example code
//escape those user variables !!
$IQuizName = mysql_real_escape_string($_POST['IQuizName']);
$IMonths = mysql_real_escape_string($_POST['IMonths']);
$result = mysql_query("SELECT * FROM questions.resultsets
WHERE QuizName= '$IQuizName'
AND PassFail= 'Pass'
AND ClientTime < NOW() ");
//don't use (or die(error)) in production code, (for now I'll let it slide).
if (!$result) then { die (mysql_error()); }
while ($row = mysql_fetch_row($result))
{
echo "the table"
}
For accurate comparing dates you should convert the dates with the strtotime function again.
In PHP there is no standard date data type. What you have is strings, and when you compare them, they get compared as strings, not as dates, thats why you get results you dont expect. How it normally is done in PHP is to format your date string by ordering from most significant bit to least bit, like
YYYY-MM-DD HH:MM:SS
If you do that, string comparison will work as expected. You can format your dates easily using:
$string = date('Y-m-d H:i:s', $year, $month, $day, $hour, $minute, $second);
So in one table I have a column of time values, just a bunch of time values like 10:15, 10:35, etc. Another column has AM/PM as a text. I'm so confused as to how to do this. How do I take those values and compare it with the current time using PHP? I know you have to do a $sql statement but then what?
Also, if it's easier for any of your answers I can add the AM/PM to the time value column so it would be 10:15 AM, 10:35 AM as a text value instead of just the numbers.
You can use the function strtotime
In particular:
<?php
$now = time();
$some_time = strtotime('11:20 PM');
if($now > $some_time)
echo "you are late";
else
echo "you have time";
?>
I have a function which checks my database to see if a date exists, if it does exist, i want to display the next date which isnt in the database.
Is this possible?
My function returns 1 if there is a date in the database and 0 if there isnt, im using codeigniter, but not using any built in functions.
Its basically an availability checker, it allows us to input many different dates in the database, so calling my function i use
$availcheck = $ci->availability->check_availability_by_date(date('d/m/Y'));
The i use a if statement to check if the first time it runs it returns a value, this is how i have it
if($availcheck > 0){
// loop through the next dates and run the function again to see if it returns 0
} else {
echo 'available now';
}
I guess i would add 1 to the current date, check that one, then add another 1 and check that and so on.
Im just not sure how.
Cheers,
if i understand you correct , your problem is adding the day ?
if so i would suggest using the epoch or unix time
so convert the date to unix time using mktime than just add 1 day in seconds (24*60*60)
and then convert back to d/m/y format.
you can use the date function.
$date = time(); // get current timestamp
while ($availcheck) // while date IS found in database
{
$availcheck = $ci->availability->check_availability_by_date(date('d/m/Y',$date));
$date = $date + (24*60*60); // add one day
}
$date = $date - (24*60*60); // reduce one day
echo date('d/m/Y',$date); // prints the first date that is not in the DB
This SQL code could work for me.
$today = date("Y-m-d"); //today
$sql = "SELECT date FROM calendar WHERE date>'{$today}' AND date<='2100-12-31' AND date='0000-00-00' LIMIT 1";
Since you can't determine the ending date, 2100 could be for testing.