Unexpected date Output - php

I wanted to add 30 days in my current order_date which is retrieved from db automatically and display the date which is 30 days ahead.But the result which i am getting is something like this 14437368002015-11-01 when the order_date was 2015-10-01.1443736800 gets Added before the date .Please help!
while($row = mysqli_fetch_array($query)){
echo '<tr>'.
'<td align="center">'.$row['order_no'].'</td>'.
'<td align="center">'.$row['order_date'].'</td>'.
'<td align="center">'.$row['store_used'].'</td>'.
'<td align="center">'.$row['status'].'</td>'.
'<td align="center">'.'Rs.'.$row['cashback'].'</td>'.
'<td align="center">'.$date = strtotime($row['order_date']);
$date = strtotime("+30 day", $date);
echo date('Y-m-d', $date);'</td>'.
'</tr>';
}
echo '</tbody></table></form>'."\n\n";
mysqli_close($dbc2);
?>
Results:
Order ID Order Date Store Used Status Cashback Est.Cashback.Date
134 2015-10-02 Flipkart To be Checked Rs. 14437368002015-11-01

It seems you have concatenation issue simply you can also directly use date function like as
'<td align="center">'.date('Y-m-d',strtotime("{$row['order_date']} +30 days")).'</td>'.
It seems from your code that your code is echoing date twice
'<td align="center">'.$date = strtotime($row['order_date']);//starting of td
and
echo date('Y-m-d', $date);'</td>'. //ending of td

This should work for you, you are not converting the date after strtotime().So convert it with date()
$date = strtotime("2015-10-01");
$date = date("Y-m-d",strtotime("+30 day", $date));
echo $date;// outputs 2015-10-31

Related

PHP format date in a dropdown not working

Im a bit stumped as i have some code to generate a simple dropdown of date for 21 days in advance.
It works fine as such however the output is not showing the right date instead its showing 1st Jan 1970
$timestamp = strtotime('today');
$output = [];
for ($day = 0; $day < 21; $day++) {
$output[] = date('d M y', strtotime(sprintf('+%d days', $day), $timestamp));
}
echo "<select name='days'>";
foreach ($output as $day)
{
echo "<option value='".$day."'>".date('l jS \of F Y',$day)."</option>";
}
echo "</select>";
if i echo just $day as the outpout value it displays in the DD/MM/YYYY format but i want it to display Day Name Date of Month Year
so whats not correct with using
date('l jS \of F Y',$day)
Im sure its something dumb but ive checked over all the various posts around on date but i cant for the life of me figure this out.
The function date() takes as its second argument a time in seconds.
If you look at the PHP:date documentation, you'll find that the second argument expects an integer, not a string.
Change your code to the following:
<?php
$output = [];
for ($day = 0; $day < 21; $day++) {
$output[] = strtotime(sprintf('+%d days', $day));
}
echo "<select name='days'>";
foreach ($output as $day)
{
echo "<option value='".$day."'>".date('l jS \of F Y', $day)."</option>";
}
echo "</select>";
Rather than filling your output array with strings, you'll see here that we instead fill it with 21 timestamps.
Once you begin looping through your output array, passing in the timestamp to the second argument of the date function will yield the output you're expecting.
I'm keeping the value of each option set to that timestamp, as it makes everything easier to work with if you need to process any POST data. But you're free to adjust the formatting there as needed.
Here's a working example that you can play around with.
Edit:
Just for funsies, here's how you can do it in a more object-oriented style. I feel it's a little more succinct this way, but it's up to you if you want to take this approach or not.
<?php
$today = new DateTime();
echo "<select name='days'>";
for ($day = 0; $day < 21; $day++)
{
echo "<option value='".$today->getTimestamp()."'>".$today->format('l jS \of F Y')."</option>";
$today->add(new DateInterval("P1D"));
}
echo "</select>";

Display data from database vertically in table

<?php
echo "<table class=\"table table-hover table-bordered\">";
echo "<thead>";
echo "<tr>";
$result=mysqli_query($con,"SELECT day FROM time_slot ORDER BY day;");
while($row=mysqli_fetch_array($result))
{
$days = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$day = $row['day']-1;
$day = $days[$day];
echo '<th>';
echo $day;
echo '</th>';
}
echo "</tr>";
echo "</thead>";
require('../includes/connection.php');
$result=mysqli_query($con,"SELECT * FROM time_slot ORDER BY day DESC;");
while($row=mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
This portion of data should be display vertically in table with respect to Day
//This code to split time into intervals
$starttime = $row['start_time']; // your start time
$endtime = $row['end_time']; // End time
$duration = $row['slot_time'];
$array_of_time = array ();
$start_time = strtotime ($starttime); //change to strtotime
$end_time = strtotime ($endtime); //change to strtotime
$add_mins = $duration * 60;
while ($start_time <= $end_time) // loop between time
{
echo '<td>';
echo "<div align=\"center\">";
print_r(date ("h:i", $start_time) ."-". date("h:i", $start_time += $add_mins));
//
echo "</br><input type=\"radio\" name=\"texi\" value=\"1\" ></input>";
echo '</td>' ;
echo '</div>';
}
echo "</tr>";
echo "</tbody>";
}
mysqli_close($con);
echo "</table>
</div>
<input type=\"submit\" value=\"Book Slot\" class=\"button_drop\">
</form>";
?>
I want to show time slots vertically with respect to day in table.
This is the output of this code:
But I want to display time which is showing horizontally that should be shown vertically. Eg. 1-3, 3-5, 5-7 of first row should be shown below Monday, then next row should be shown below Tuesday and so on.
Try using a query like this:
SELECT day FROM time_slot ORDER BY time_slot, day;
That should get the data in an easier order to process. I can't see your exact data structure but it looks like you should get 7x 01:00-03:00, then 7x 03:00-05:00 etc. Each set should be in order by day. Then you can just write out your <td>s in the order the data comes from the DB. Keep track of when the day value changes (or just count up to 7) and start a new <tr> at that time.

how to find out missing date in mysql using php

How to find out missing date in MySQL using PHP?
echo "<tr> <th>Username</th><th>Date</th><th>Check In</th><th>Check Out</th> </tr>";
// get results1 from database
$result1 = mysql_query("SELECT * FROM attend WHERE user_name='ali' AND date BETWEEN '2015-07-01' AND '2015-07-15' order by date");
while($row = mysql_fetch_array($result1))
{
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['user_name'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['checkin'] . '</td>';
echo '<td>' . $row['checkout'] . '</td>';
echo "</tr>";
}
echo "</table>";
currently result
Username Date Check In Check Out
ali 2015-07-01 11:30:34 17:23:47
ali 2015-07-02 10:11:34 17:15:15
ali 2015-07-03 09:32:34 18:16:27
ali 2015-07-06 10:41:34 16:56:13
ali 2015-07-07 08:51:34 17:36:01
ali 2015-07-08 05:61:34 17:16:26
ali 2015-07-09 04:11:34 17:14:12
ali 2015-07-10 02:81:34 17:25:25
ali 2015-07-13 11:71:34 17:02:29
ali 2015-07-14 10:81:34 17:04:20
ali 2015-07-15 09:31:34 17:00:43
and i want result like this
Username Date Check In Check Out
ali 2015-07-01 11:30:34 17:23:47
ali 2015-07-02 10:11:34 17:15:15
ali 2015-07-03 09:32:34 18:16:27
2015-07-04
2015-07-05
ali 2015-07-06 10:41:34 16:56:13
ali 2015-07-07 08:51:34 17:36:01
ali 2015-07-08 05:61:34 17:16:26
ali 2015-07-09 04:11:34 17:14:12
ali 2015-07-10 02:81:34 17:25:25
2015-07-11
2015-07-12
Ali 2015-07-13 11:71:34 17:02:29
ali 2015-07-14 10:81:34 17:04:20
ali 2015-07-15 09:31:34 17:00:43
All of your missing check ins are not assigned to any user. If you want to fetch records assigned to user ali or unassigned, between gives dates:
SELECT * FROM attend WHERE (user_name='ali' OR user_name = '') AND date BETWEEN '2015-07-01' AND '2015-07-15' order by date
It would be much cleaner if you show us your schema.
I'd use timestamp for that date column. If you do this, you can simply "check" which day is expected to come next. If its not that day, print that day like you wanted and then continue, if you got that expected date.
Do do so, you should save all your timestamps at a given time of that day. Like always 12:00:00. This makes handling those numbers much easier.
echo "<tr> <th>Username</th><th>Date</th><th>Check In</th><th>Check Out</th> </tr>";
// get results1 from database
$iLastTimestamp = 0
$result1 = mysql_query("SELECT * FROM attend WHERE user_name='ali' AND date BETWEEN '2015-07-01' AND '2015-07-15' order by date");
while($row = mysql_fetch_array($result1))
{
if ($iLastTimestamp != 0)
{
while ($iLastTimestamp != $row['date'])
{
// echo out the contents of each row into a table
echo "<tr>";
echo '<td> </td>';
echo '<td>' . date("Y-m-d", $iLastTimestamp) . '</td>';
echo '<td> </td>';
echo '<td> </td>';
echo "</tr>";
$iLastTimestamp = strtotime("+1 Day 12:00:00", $iLastTimestamp)
}
}
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['user_name'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['checkin'] . '</td>';
echo '<td>' . $row['checkout'] . '</td>';
echo "</tr>";
}
echo "</table>";
Another way is to convert your date into an timestamp first, before using the code I posted. You can use the "strtotime"-Func here, too. Just add the time, just to be sure. :)
You can do this with a range in a function, this will permit you to reuse your code:
function getAttendInInterval($begin,$end) {
$begin = new DateTime( $begin );
$end = new DateTime( $end );
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ) {
$result = mysql_query('SELECT * FROM attend WHERE user_name="ali" AND date = '".$dt->format( 'Y-m-d' )."' order by date');
...
}
This fonction permits to show the attend for each day in an interval.
and then:
getAttendInInterval('2015-07-01','2015-07-15');
you can use date in LOOP and if date is match in database then show all info. of database with date otherwise show only date.

Minus 7 days from MYSQL date variable in 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');

loop array values

Here I access 'date' key values from rows of DB table. And I can echo these values, no problem.
$res = $mysqli->query("SELECT * FROM alfred ORDER BY id ASC");
$row = $res->fetch_all(MYSQLI_ASSOC);
foreach ($row as $key => $value){
$availDate = $value['date'];
echo $availDate.'<br />';
}
This loop above shows all 'date' values from DB, in this case there are 3 dates- "2012-09-25" "2012-09-27" and "2012-09-29".
But then I need to compare each of these 'date' values against values of $date->format('Y-m-d') from the code below and display each date with corresponding "busy" or "available" status into separate <td> of the table. My following version compares only the "last" value of 'date' key - "2012-09-29", but I need to compare each 'date' value from the array above, it means also "2012-09-25" and "2012-09-27". I have tried many versions but still unsuccessful. Any ideas?
$date = new DateTime();
$endDate = new DateTime('+10 day');
for($date->format('Y-m-d'); $date->format('Y-m-d') < $endDate->format('Y-m-d'); $date->modify('+1 day')){
if ($date->format('Y-m-d') == $availDate){
echo '<td>'.$date->format('Y-m-d/D').' busy</td>';
} else {
echo '<td>'.$date->format('Y-m-d/D').' available</td>';
}
}
Here is the result I am getting now:
2012-09-21/Fri available 2012-09-22/Sat available 2012-09-23/Sun available 2012-09-24/Mon available 2012-09-25/Tue available 2012-09-26/Wed available 2012-09-27/Thu available 2012-09-28/Fri available 2012-09-29/Sat busy 2012-09-30/Sun available
But in fact I need to show "busy" status also into <td> of "2012-09-25" and <td> of "2012-09-27" as these also are 'date' values that are existing in $row array. Unfortunately I can not post any images here to show, but I hope my result above gives you the idea.
SOLVED with the help of in_array below:
$aAvailDate = array();
foreach ($row as $key => $value){
$aAvailDate[] = $value['date'];
}
$date = new DateTime();
$endDate = new DateTime('+10 day');
for($date->format('Y-m-d'); $date->format('Y-m-d') < $endDate->format('Y-m-d'); $date->modify('+1 day')){
if (in_array($date->format('Y-m-d'), $aAvailDate)){
echo '<td>'.$date->format('Y-m-d/D').' busy</td>';
} else {
echo '<td>'.$date->format('Y-m-d/D').' available</td>';
}
}
I haven't tested your code, but I think you are running ->format('Y-m-d') unnecessarily here, and this is messing up your logic.
Every time you run that, PHP is turning your object into a string, which you are then comparing against other strings. This won't do anything useful.
Instead, you should be using the features of the DateTime class to compare the objects themselves. The only time you should need to use the format() method is when outputting to the browser, into an SQL query, etc
Although your Question is Unclear but AFAIK
you want to display "Busy" if available date occurs between given date upto 3 Weeks
otherwise display "free"
I would like to suggest you to do this with MySQL (Not tested)
SELECT *,
IF( `DateCol` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 3 WEEK), 'Busy','Free')
AS status
FROM TableName
Try a while loop instead of a foreach. Also, compare the DateTime objects directly, not the formatted strings.
$date = new DateTime();
$endDate = new DateTime('+3 week');
while( $date < $endDate) {
if ($date->format('Y-m-d') == $availDate){
echo '<td class="busy">busy</td>';
} else {
echo '<td>free</td>';
}
$date->modify("+1 day");
}
Something like this? (If I understand what you're trying to do correctly)
<?php
$avail_dates = array();
$res = $mysqli->query("SELECT DATE_FORMAT(date, '%Y-%m-%d') AS availDate FROM alfred ORDER BY id ASC");
$row = $res->fetch_all(MYSQLI_ASSOC);
foreach ($row as $key => $value){
$avail_dates[] = $value['availDate'];
}
$startDate = date('Y-m-d');
$endDate = date('Y-m-d', strtotime(date("Y-m-d", strtotime($startDate)) . " +3 week"));
?>
<table>
<?php
foreach ($avail_dates as $availDate){
echo "<tr><td>$availDate</td>";
if (($startDate <= $availDate) && ($endDate >= $availDate)){
echo "<td class='busy'>busy</td>";
}else{
echo "<td>free</td>";
}
echo "</tr>";
}
?>
Instead of printing values, I would add them to the array, and then run a loop on that array, comparing the values to the given start and end dates. I also wouldn't fetch all from the table if you nly need a date.
May be like this?
$date = new DateTime();
$endDate = new DateTime('+3 week');
for($date->format('Y-m-d'); $date->format('Y-m-d') < $endDate->format('Y-m-d'); $date->modify('+1 day')){
$tempDate = $date->format('Y-m-d');
if ($tempDate === $availDate){
echo '<td class="busy">busy</td>';
} else {
echo '<td>free</td>';
}
}

Categories