Minus 7 days from MYSQL date variable in PHP? - php

I have a script that counts how many days have past since the start date stored in my database.
The theory is, a user signs up and has 7 days to complete registration, from the admin side of things, this script is used to monitor where a user is upto and how many days have past since they registered.
So in this example we give the user 7 days grace in which to complete. This echos out like '(number of days out of 7 have past'
Then after 7 days this should change to give the number of days overdue. so if a user has 7 days to complete and they take 8 days then this will take into account the first 7 days grace and then have an overdue date of 1 day.
so for instance it should echo out '1 day overdue', rather than what it is currently doing and saying 8 days overdue, i am trying to do this by minusing the first 7 days.
i have been told this will help however i am having trouble getting it to work with my date variable
$pre_date=date('Y-m-d', strtotime('-7 days'));
heres my compelte code, please can someone show me where i am going wrong
<?php include 'config.php';
$data = mysql_query("SELECT *,
TIMESTAMPDIFF(DAY, date, CURDATE()) AS expire_date
FROM supplier_session
ORDER BY expire_date ASC")
or die(mysql_error());
echo "<table class=\"table\" style=\"width:995px; font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
font-size:11px;\" >
<tr>
<td style=\"width:100px;\">ID:</td><td>Company Name:</td><td>Company Reg No:</td><td>Application Started:</td><td style=\"width:200px;\">Application Duration:</td><td style=\"width:100px;\">Date:</td><td>Status:</td></tr>";
while($row = mysql_fetch_array( $data )) {
$days = $row['expire_date'];
$when = $days*0;
$str = $row['expire_date'];
$str2 = substr($str, 0); // "quick brown fox jumps over the lazy dog."
$pre_date=date('Y-m-d', strtotime('-7 days'));
if ($when <= 31){
echo "<tr><td style=\"width:100px;\"><p>".$row['id'] . "</p></td>";
echo "<td style=\"width:150px;\"><p>".$row['company_name'] . "</p></td>";
echo "<td style=\"width:150px;\"><p>".$row['company_reg_number'] . "</p></td>";
echo "<td>"; echo date('d/m/Y',strtotime($row['date'])); echo "</td>";
if ($days >= 8) {
echo "<td style=\"width:200px;\"><p>{$pre_date} days overdue</td>";
}
elseif ($when <= 7){
echo "<td style=\"width:200px;\"><p>{$str2} of 7 days past</td>";
}
}
echo "<tr>";
}
echo "</table>"; //Close the table in HTML
?>

You've forgotten to pass the date variable to strtotime, so you are always substracting 7 days to the actual date and getting 8 as a result.
Try with a subraction of the actual date minus the expire date in order to get the number of days that have past.

I think u have some mistake in ur function
$pre_date=date('Y-m-d', strtotime('-7 days'));
For example u can try to use somth like that
$datetime = date("Y-m-d", strtotime( date( 'Y-m-d' )." +2 days"));
Result of function looks like:
2014-05-08
Also there more information what u need:
Click.
With unixtime looks like that. We take difference between 2 dates in second and divide it on number of seconds in day
<?php
date_default_timezone_set('UTC');
$now_time = time();
$date_before = time()-(7*24*60*60);// 7 days; 24 hours; 60 mins; 60secs
echo($now_time);
echo('<br>');
echo($date_before);
echo('<br>');
$difference = $now_time - $date_before;
$result = intval($difference/(24*60*60)); //seconds in day
echo($difference);
echo('<br>');
echo($result);
also u can get time from date with:
$unixtime = strtotime('22-09-2008');

Related

Display the first and last day of a (calendar) week

In my database I have a table with consecutive dates. I group all dates to calendar weeks and display them in a table in my CSS.
Now i want to display also the first and the last day of the calendar week in my table.
Here is my code:
$statement = $pdo->prepare("SELECT WEEK(Date), Date FROM table GROUP BY WEEK(Date)");
$result = $statement->execute();
$count = 1;
while($row = $statement->fetch()) {
echo "<tr>";
echo "<td>".$row['WEEK(Date)']."</td>";
echo "<td>".$row['Date']."</td>";
Currently there is only shown the first day of the calendar week.
I thought of duplicating $row['Date']and adding +6 (days) but the first dataset is not the first day of the week. So the first date range of the first calendar week would be wrong.
Does anyone has an idea how to solve this problem?
See a test of this here: https://www.tehplayground.com/c2dOYxxwIa9LDscW
while($row = $statement->fetch()) {
$thedate = $row['Date'];
$dow = date('w', strtotime($thedate)); // the day of the week of the date - number based - zero is sunday
$dowsun = date('Y-m-d', (strtotime($thedate) - ($dow * 60*60*24))); // first day of that week (sunday)
$dowsat = date('Y-m-d', strtotime($thedate) + ((6-$dow) * 60*60*24)); // last day of that week (saturday)
/* for example:
if $row['WEEK(Date)'] = "2021-06-08" which is a Tuesday
$dowsun = "2021-06-06" which is a Sunday
$dowsat = "2021-06-12" which is a Saturday
with this answer you get the full week that contains the target date
*/
echo "<tr>";
echo "<td>".$row['WEEK(Date)']."</td>";
echo "<td>".$row['Date']."</td>";

PHP - Populate select options with 15 min time intervals

Hi I am creating a website for a restaurant delivery service. Basically when the customer is checking out, he/she can choose when they want the food to be delivered. I want the select box to contain time intervals of 15 mins ranging from the current time until the close time. The restaurant is open for deliveries between 11:00 to 23:00. The first option I want is to be "As soon as possible", and then the next option is an hour later (rounded to nearest 15mins), and then 15 mins each time. So basically something like this:
(Suppose current time is 13:55)
As soon as possible
15:00
15:15
15:30
15:45
...and so on until close time (23:00)
If the restaurant is closed (after 23:00) then I just want the select box to have an option that says "CLOSED".
Here is what I have tried so far but it is not working:
<select>
<?php
$timenow = date("H:i");
if($timenow >"23:00" || $timenow < "11:00"){
echo '<option value="Closed">CLOSED</option>';
echo "</select>";
}
else{
$deliverytime = date("H:i", strtotime('+15 minutes', $timenow));
echo '<option value="asap">As soon as possible</option>';
while($deliverytime < "23:00" && $deliverytime > "11:00"){
echo '<option value="'. $deliverytime.'">' . $deliverytime . '</option>';
$deliverytime = date("H:i", strtotime('+15 minutes', $deliverytime));
}
echo "</select>";
}
?>
strtotime('+15 minutes', $timenow) is not correct. The second argument should be a timestamp, not a string. You want something like strtotime('+15 minutes', time()) or just leave off the second argument (current time is the default).
A better approach is to always work with the timestamps until you output. That makes rounding and comparisons much easier.
<select>
<?php
$timenow = time();
$opentime = strtotime('11:00');
$closetime = strtotime('23:00');
if($timenow > $closetime || $timenow <= $opentime){
echo '<option value="Closed">CLOSED</option>';
echo "</select>";
}
else{
// you said you wanted the time to start in 1 hour, but had +15 minutes...
$deliverytime = strtotime('+1 hour', $timenow);
// round to next 15 minutes (15 * 60 seconds)
$deliverytime = ceil($deliverytime / (15*60)) * (15*60);
echo '<option value="asap">As soon as possible</option>';
while($deliverytime <= $closetime && $deliverytime >= $opentime) {
echo '<option value="'. date('H:i', $deliverytime) .'">' . date('H:i', $deliverytime) . '</option>'."\n";
$deliverytime = strtotime('+15 minutes', $deliverytime);
}
echo "</select>";
}
I can't provide a complete solution but can point you in the right direction.
This code will handle the 15 minute interval parts for you:
$start = new DateTime();
$end = new DateTime('11PM');
$interval = new DateInterval('PT15M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
echo $dt->format("l Y-m-d") . PHP_EOL;
}
The accepted answer in this Stack Overflow question shows how to get to the nearest 15 minute interval: PHP DateTime round up to nearest 10 minutes
Combine the two and you should have a complete solution to your problem.

Get the number of days till today for the current month and current year

How To get the number of days that have already passed from the current month and the number of days that have already passed from the current year eg if today is April 6.
I should be getting 6 for current month and
I should be getting 97 for current year
Is there a way I can get the same excluding SUNDAYS
Take a look at the php date() function. It has a parameter 'z', which is the day of the year, in your situation 97:
echo date('z');
Same with day of the month:
echo date('j');
You need to use getdate function
var_dump( getdate() );
Will output:
array(11) {
...
["mday"]=>
int(6)
...
["yday"]=>
int(96)
...
}
PHP has excellent documentation about this:
http://php.net/manual/en/function.date.php
The current month in php can be retrieved with date("n") and the current day of the year is date("z") (starting with zero).
$dayNumber = date("z") + 1;
date("z") starts counting from 0
this will surely help you
<?php
$dt = new DateTime;
if(isset($_GET['year']) && isset($_GET['week'])) {
$dt->
setISODate($_GET['year'], $_GET['week']);}
else {
$dt->setISODate($dt->format('o'), $dt->format('W'));
}
$year = $dt->format('o');
$week = $dt->format('W');
?>
<a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week- 1) .
'&year='.$year; ? >">Pre Week</a>
<a href="<?php echo $_SERVER['PHP_SELF'].'?week='.
($week+1).'&year='.$year;
? >">Next Week</a>
<table>
<tr>
<td>Employee</td>
<?php
do{
echo "<td>" . $dt->format('l') . "<br>" . $dt->format('d M Y') . "
</td>\n";
$dt->modify('+1 day');
}while ($week == $dt->format('W'));
?>

select menu with current date

How do you create a select with the option of 2 days ahead from today picked as the default option (i.e. a 48 hour window) in PHP? This is the code I'm using so far but it doesn't work unfortunately!
<?php
$weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$days = range (1, 31);
$currentDay = date('F');
echo "<select name='weekday'>";
foreach ($days as $value) {
$default = ($value == $currentDay)?'selected="selected"':'';
echo '<option '.$default.' value="'.$value.'">'.$value."</option>\n";
}
echo '</select> ';
?>
I'm confused as to what your code does.
As far as I can tell $weekday is not used after being instantiated, and you are setting $currentDay to the text representation of the current month (e.g. September).
But if you want to get the day of month of the day 48 hours from now:
$two_days_ahead = date('j', strtotime('+ 48 hours'));

Find a specific day of week based on a date

I would like to be able to find the date which "Sunday" falls on for a given date, formatted as YYYY-MM-DD.
... How would this be achieved in PHP?
strtotime() may help you, it accepts GNU-Date-input-formats like "next Sunday"
echo date('Y-m-d',strtotime('sunday',strtotime("2011-07-04 Z")));
Look at the time function for php.
If you can't find a library that does this, then:
Come up with a reference date (January 1, 1970?) for which you know the day of the week.
Calculate the number of days between the reference date and your given date.
Take that answer modulo 7.
Convert to a day of the week.
Here you have a full example with multiples values
it is for PHP or any other lenguage that uses epoch time (or similar)
<?php
function prevSunday($paramDay){
$aDay = 24*60*60;
$aWeek = $aDay * 7;
$sunday = $aDay + $paramDay - (($paramDay - 3 * $aDay) % $aWeek);
return $sunday;
}
$randDay = 1309819423 + rand(0, 9000000); // this contanis a random day (and hours, minutes, seconds)
$aSaterday = strtotime("20110702");
$aSunday = strtotime("20110703");
$aMonday = strtotime("20110704");
echoTwoDays($aSuterday, prevSunday($aSuterday));
echoTwoDays($aSunday, prevSunday($aSunday));
echoTwoDays($aMonday, prevSunday($aMonday));
/* echoes
day1: Saturday 20110702
day2: Sunday 20110626
day1: Sunday 20110703
day2: Sunday 20110703
day1: Monday 20110704
day2: Sunday 20110703
*/
echoTwoDays($randDay, prevSunday($randDay)); // echoes a random date and perv sunday
function echoTwoDays ($day1, $day2) {
echo "day1: " . date("l Ymd", $day1) ."<br>"; // remove l as your request
echo "day2: " . date("l Ymd", $day2) ."<br><br>"; // remove l as your request
}
function prevSunday($paramDay){
$aDay = 24*60*60;
$aWeek = $aDay * 7;
$sunday = $aDay + $paramDay - (($paramDay - 3 * $aDay) % $aWeek);
return $sunday;
}
?>

Categories