I have a database with rows of employee information. In the data I have training dates that expire. I need to compare the expiration date that is pulled to the current date. I'd like to bold and turn red the date if it is within 30 days of expiring. So anytime the page is loaded if it is within that 30 day time frame it will stand out.
This is what I have currently without the if statement I am looking for:
$lic_ex = date("m/d/Y", strtotime($row['lic_ex']));
echo "<td>" . $lic_ex . "</td>";
Convert expiration date in DateTime object.
Create DateTime object which represents today date + 30 days.
$thirtyDaysLater = (new \DateTime('today'))->modify('+30 day');
Compare both objects and if DateTime object from 2. is bigger, then the person is in 30 days expiration period and you can bold the text
For example:
$expirationDate= DateTime::createFromFormat('m/d/y' , $input);
$compareDate = (new \DateTime('today'))->modify('+30 day');
if ($compareDate >= $expirationDate) {
echo "<td><strong>" . $lic_ex . "<\strong></td>";
} else {
echo "<td>" . $lic_ex . "</td>";
}
just adjust the createFromFormat parameters to your data
Related
I'm using the Bittrex REST API to find trades that occurred less than ten minutes ago:
https://bittrex.com/api/v1.1/public/getmarkethistory?market=BTC-ETH
Here are some of the trade timestamps that are returned from the endpoint:
2018-09-23T04:47:07.237
2018-09-23T04:47:02.797
Although today is actually 2018-09-22 where I live, it's showing these trades occurring in the future. Is the timezone different or something for these timestamps?
$now = date('m/d/y g:i a');
$now = strtotime($now);
$ten_minutes_ago = strtotime('-10 minutes');
foreach ($buy_sell_bittrex_orders['result'] as $buy_sell_bittrex_order) {
$buy_sell_bittrex_order_timestamp = $buy_sell_bittrex_order['TimeStamp'];
echo "Ten Minutes ago: " . $ten_minutes_ago . "<br>";
echo "Buy sell timestamp: " . $buy_sell_bittrex_order_timestamp . "<br>";
echo "Now: " . $now . "<br><br>";
if ( strtotime($buy_sell_bittrex_order_timestamp) >= $ten_minutes_ago && strtotime($buy_sell_bittrex_order_timestamp) <= $now ) {
// Do something
}
}
When I use the code above, none of the trades are found within 10 minutes ago, although there certainly must be. There can't have been trades that occurred in the future, so is there a timestamp issue here?
Here's an example of what the code above outputs:
Ten Minutes ago Bittrex: 1537679309
Buy sell timestamp: 1537704500
Now: 1537679940
In the example above, "Buy sell timestamp" is a higher value than "now" which represents the current day/time.
How can I convert the trade timestamps to match my current timezone? That seems to be the issue, though I may be wrong. Thanks!
You should provide the original Timezone and convert it to your Timezone.
See this Stackoverflow question and answer.
Convert time and date from one time zone to another in PHP
Good luck.
I'm working on a time management system and am currently stuck. I'm trying to use PHP to calculate the number of hours/minutes between two columns (from and to). These are both set as 'time' type so MySQL recognises it as a time datatype.
I've tried the following in PHP:
$from= isset($_GET['from']) ? '%'.$_GET['from'].'%' : '';
$to = isset($_GET['to']) ? '%'.$_GET['to'].'%' : '';
$diff = $to - $from;
echo "<table border='1'>
echo "<tr>";
echo "<td>" . $row[$diff] . "</td>";
echo "</tr>";
echo "</table>";
Say for example 'to' is set at 15:00:00 and from is set at 09:00:00, then the time difference for that row should be 6 hours and this should be displayed for that row.
The variable $difference is supposed to echo the difference in hours/minutes but it just displays the id assigned to that row instead. I'm trying to echo the number of minutes for each row in the table not just one.
I've seen the timediff function but don't know if that will do the trick.
I don't see your logic with the % and the $_GET vars, and $row suddenly appears out of nowhere.
You should convert the inputted times to php DateTime classes.
An example shows calculating diffs: http://nl1.php.net/manual/en/class.datetime.php#108970
Create your DateTime for example like this:
$dateFrom = new DateTime("2014-02-24 ".$_GET['from']);
$dateTo = new DateTime("2014-02-24 ".$_GET['to']);
$diff = $dateFrom->diff($dateTo);
print_r($diff) ;
The day you use is not really important if you only need the difference in time.
I'm a very rookie programmer when it comes to PHP. I can usually manage most problems that I run into, but with this one I'm stumped.
I'm trying to create a program where a user can insert a name into a table, and 30 minutes after insertion the name would be flagged as "expired". I'm able to create the row in MySql and using MySql I can put the time when the name was entered. Now what I'm trying to do is take that time from the database and check if it's been 30 minutes since it was entered.
I figure I could do this by taking the time from the database, adding 30 minutes to it, then checking against the current time to see if its past it yet.
This is what I have so far. The time is in this format: 2013-08-21 13:18:35
$result = mysqli_query($con,"SELECT * FROM list ORDER BY id desc");
//snip snip
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['reason'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
cho "</tr>";
}
Now I know that I need to take $row['date'] and add 30 minutes to it. I've read probably about 20 different pages about this, but nothing I do seems to work.
I've seen a few different things suggest such as date_add() and strtotime but either I'm doing something wrong, or it doesn't work.
If I could just get a working code sample for how to add 30 minutes to $row['date'], I'm sure I could explain it to myself.
I know how to compare the two once I've got the 30 minutes added, and I think this would be correct:
if ($time30 < $now) {
// Code to execute
}
Any help would be appreciated.
Considering that $row['date'] prints out 2013-08-21 13:18:35. you need to use strtotime() to convert that value into a timestamp.
See documentation here strtotime.
Now that you have a timestamp, you simply add 1800 seconds to it (half an hour) and you obtain what you need.
Then, a simple comparison between your time and now (you obtain the value with time() ) will give you the result.
This code is a mere example (even too much detailed, because it can be shorter):
$db_time = strtotime("2013-08-21 20:18:35");
$half_hour = 1800;
$time = $db_time + $half_hour;
if($time < time()) echo "expired"; else echo "active";
First, avoid select *
Mysql solution would be to return the time after 30 mins as a column
SELECT TIMESTAMPADD(MINUTE,30,date1);
Assuming date1 is your column name avoid using date as a column name, at best its a maintenance issue
In PHP:
$time30 = strtotime($row['date']) + (30 * 60);
$now = time();
In MySQL
SELECT *,timestampadd(minute,30,date)<now() as expired FROM list ORDER BY id DESC
Then in the PHP you have $row['expired'] as a 1 or 0.
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'm writing a php script that iterates over the Monday of each week.
However the script seemed to get out of sync after 22nd of October.
<?php
$october_8th = strtotime("2012-10-08");
$one_week = 7 * 24 * 60 * 60;
$october_15th = $october_8th + $one_week;
$october_22nd = $october_15th + $one_week;
$october_29th = $october_22nd + $one_week;
$november_5th = $october_29th + $one_week;
echo date("Y-m-d -> l", $october_8th) . '<br />';
echo date("Y-m-d -> l", $october_15th) . '<br />';
echo date("Y-m-d -> l", $october_22nd) . '<br />';
echo date("Y-m-d -> l", $october_29th) . '<br />';
echo date("Y-m-d -> l", $november_5th) . '<br />';
This would output:
2012-10-08 -> Monday
2012-10-15 -> Monday
2012-10-22 -> Monday
2012-10-28 -> Sunday
2012-11-04 -> Sunday
I would expect it to say the 29th of October but it gets stuck at the 28th.
How should I get around this problem?
A preferred choice would be to use PHP's date-related classes to get the dates.
These classes importantly handle the daylight-savings boundaries for you, in a way that manually adding a given number of seconds to a Unix timestamp (the number from strtotime() that you used) cannot.
The following example takes your start dates and loops four times, each time adding a week to the date.
$start_date = new DateTime('2012-10-08');
$interval = new DateInterval('P1W');
$recurrences = 4;
foreach (new DatePeriod($start_date, $interval, $recurrences) as $date) {
echo $date->format('Y-m-d -> l') . '<br/>';
}
PHP Manual links:
The DatePeriod class
The DateInterval class
The DateTime class
While writing this question I discovered that day light saving time ends at the 28th of October.
Because the date at initialization doesn't contain a specific time automatically midnight is assigned. This however yields a problem when summertime ends. Suddenly the time isn't midnight anymore but one hour before that AND thus a day earlier then you would expect.
An easy fix would be to initialize the time to be midday instead of midnight:
$october_8th = strtotime("2012-10-08 12:00");
Perhaps there might be more elegant solution (you're welcome to leave one), but this will do for this purpose.