...

View Full Version : strtotime +1 day



nickburrett
10-25-2012, 09:55 PM
I have a date stored in my MySQL database: 2012-10-27 19:00:00

When I use the following PHP code I get the date Saturday 27th October 2012 instead of Sunday 28th October 2012.

Can anyone tell me what I'm doing wrong?


<?php echo date("l jS F Y", strtotime($start_time,'+1 day')); ?>

Inigoesdr
10-26-2012, 01:19 AM
You have the arguments backwards and the input for strtotime() (http://php.net/strtotime) is expecting a timestamp, so you need to pass it through strtotime() (http://php.net/strtotime) itself, or use the MySQL UNIX_TIMESTAMP() (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp) function when you request the field from the database so it gets returned as a unix timestamp instead of a MySQL date.

$start_time = '2012-10-27 19:00:00';

echo date('l jS F Y', strtotime('+1 day', strtotime($start_time))); // Sunday 28th October 2012

This also works:

echo date('l jS F Y', strtotime($start_time . ' +1 day'));
If you have PHP >= 5.3 you should consider using the DateTime class' add() (http://php.net/datetime.add) function.

nickburrett
10-26-2012, 02:16 PM
Thank you...

How would I apply that fix here?


$row .= '<tr>

<td>'.$start_time.'</td>
<td>'.$client_first.' '.$client_last.'</td>
<td>'.$dj_requested.'</td>
<td>'.$quotation_status.'</td>
<td><a href="update.php?id='.$id.'">Update</a></td>
<td><a href="view.php?id='.$id.'">View</a></td>
<td><a href="signature.php?id='.$id.'">Signature</a></td>
<td><a href="delete.php?id='.$id.'">Delete</a></td>
</tr>';

nickburrett
10-31-2012, 11:47 AM
Hello

Could someone please tell me why my date is not displaying correctly. I'm assuming it's got something to do with $row but I don't know how to fix it?


<?php include 'header.php'; ?>

<table width="100%" border="1">

<?php

// Make a MySQL Connection
include("connect.php");

// Make a MySQL Connection
$query = "SELECT * FROM bookings ORDER BY start_time";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){

?>

<tr>
<td><?php echo date("l jS F Y", strtotime($start_time)); ?></td>
<td><?php echo date("g:i a", strtotime($start_time)); ?> - <?php echo date("g:i a", strtotime($finish_time)); ?></td>
<td><?php echo $row['client_first'] ?></td>
<td><?php echo $row['client_last'] ?></td>
<td><?php echo $row['dj_requested'] ?></td>
<td><?php echo $row['quotation_status'] ?></td>
<td><a href="update.php?id=<?php echo $row['id'] ?>">Update</a></td>
<td><a href="view.php?id=<?php echo $row['id'] ?>">View</a></td>
<td><a href="signature.php?id=<?php echo $row['id'] ?>">Signature</a></td>
<td><a href="delete.php?id=<?php echo $row['id'] ?>">Delete</a></td>
</tr>

<?php
mysql_close();
}
?>

</table>

<?php include 'footer.php'; ?>

Fou-Lu
10-31-2012, 03:25 PM
What does it display as? I'd expect it'd be either January 1, 1970 or December 31, 1969 (check by adding F Y j to the date) at midnight. $start_time and $finish_time simply do not exist here, so you are saying that you want time 0, which is midnight between December and January on the 1969-1970 roll. Are these dates supposed to come from $row? The only thing we can tell here is that you for sure have a start_time in that table since you are ordering by it, but other than that we haven't a clue this structure.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum