PHP/MySQL - Time and Date issues

hey all
i have a table that has 2 fields
length (duration in 10080 format)
created(date in 1391655929 format)

what i am trying to do is display it like so

Created
01/25/2014 4:30pm

End (Created date + length)
01/26/2014 4:30pm

i have seen sooooo many differant php info on time and date that i am forever confused on the subject can you please help.

[php]<?php

date_default_timezone_set (‘Europe/Oslo’);

$date = 1391655929;
$length = 10080;

echo 'Created: ’ . date(“m/d/Y g:ia”, $date) . ‘
’;
echo 'End: ’ . date(“m/d/Y g:ia”, $date+$length);[/php]

List of supported timezones
http://www.php.net/manual/en/timezones.php

hey jim thanks for the reply so i got that working but now i am having issues calculating time left until we reach the expired time…

the values:

created = 1391876271
length = 1

my code:
[php]
.row0 {
background-color: #666;
}
tr.row0:hover {
background-color: #ccc;
}
.row1 {
background-color: #333;
}
tr.row1:hover {
background-color: #ddd;
}

<?php date_default_timezone_set ('America/New_York'); $rowclass = 0; function conv2sec($seconds) { $y = floor($seconds / (86400*365.25)); $d = floor(($seconds - ($y*(86400*365.25))) / 86400); $h = gmdate('H', $seconds); $m = gmdate('i', $seconds); $s = gmdate('s', $seconds); $string = ''; if($y > 0) { $yw = $y > 1 ? ' Years ' : ' Year '; $string .= $y . $yw; } if($d > 0) { $dw = $d > 1 ? ' Days ' : ' Day '; $string .= $d . $dw; } if($h > 0) { $hw = $h > 1 ? ' Hrs ' : ' Hr '; $string .= $h . $hw; } if($m > 0) { $mw = $m > 1 ? ' Mins ' : ' Min '; $string .= $m . $mw; } if($s > 0) { $sw = $s > 1 ? ' Secs ' : ' Sec '; $string .= $s . $sw; } return preg_replace('/\s+/',' ',$string); }

$con=mysqli_connect(“xxxx”,“xxxx”,“xxxx”,“xxxxx”);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,“SELECT * FROM table”);
?>

<?php while($row = mysqli_fetch_array($result)) { $rowclass = 1 - $rowclass; $created = $row['created']; //date created as timestamp (seconds) $length = $row['length']*60; //input is mins so we need to *60 to get seconds $ends = $created+$length; //calc expire date as timestamp (seconds) $diff = time()-$ends; //calc diff from now till expire $res = conv2sec($diff); //format seconds of diff to time output ?> <?php } ?>
Name ID Reason Expires By When Time Left
<?php echo $row['name']; ?> <?php echo $row['id']; ?> <?php echo $row['reason']; ?> <?php echo date("m/d/Y g:ia", $ends); ?> <?php echo $row['by']; ?> <?php echo date("m/d/Y g:ia", $created); ?> <?php echo $res; ?>
<?php mysqli_close($con); ?>
[/php]

Displays: 01 hr 20 mins 05 secs

expected result: 59 secs

the 1 in length is in minutes and its reading anything under 60 as hours if i change length to 60 i get a display of
01 hr 23 mins 15 secs
and now it reads the 60 as mins …

i am confuzzeled!!! :’( :’( :’(

Any ideas?

Something like this?

[php]<?php
date_default_timezone_set (‘Europe/Oslo’);

$eventCreated = 1391974216;
$eventLength = 10080;

$created = new DateTime();
$created->setTimestamp($eventCreated);

$end = new DateTime();
$end->setTimestamp($eventCreated+$eventLength);

$now = new DateTime();
$timeUntilEnd = $now->diff($end);

echo 'Created: ’ . $created->format(‘m/d/Y g:ia’) . ‘
’;
echo 'End: ’ . $end->format(‘m/d/Y g:ia’) . ‘
’;
if ($now < $end) {
echo 'Time left: ’ . $timeUntilEnd->h . 'h, ’ . $timeUntilEnd->i . 'm, ’ . $timeUntilEnd->s . ‘s’;
} else {
echo ‘Event has ended.’;
}[/php]

Output:

Created: 02/09/2014 8:30pm End: 02/09/2014 11:18pm Time left: 2h, 47m, 51s

:smiley: Yup just like that but now when i narrow my select statement with

[php]$today = $_SERVER[‘REQUEST_TIME’];
date_default_timezone_set (‘America/New_York’);
$result = mysqli_query($con,“SELECT * FROM flagged WHERE created <= $today”);[/php]

it still shows all records not just the ones that haven’t expired. (trying to not display the expired records)

Full Code:
[php]
.whtxt{
color:#FFF;
}
.row0 {
background-color: #666;
}
tr.row0:hover {
background-color: #ccc;
}
.row1 {
background-color: #333;
}
tr.row1:hover {
background-color: #ddd;
}

<?php $con=mysqli_connect("","","","");
if (mysqli_connect_errno())
{
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$today = $_SERVER['REQUEST_TIME'];
date_default_timezone_set ('America/New_York');
$result = mysqli_query($con,"SELECT * FROM flagged WHERE created <= $today");
$rowclass = 0;
?>
<?php while($row = mysqli_fetch_array($result)) { $eventCreated = $row['created']; $mins = $row['length']; $eventLength = $mins*60; $created = new DateTime(); $created->setTimestamp($eventCreated); $end = new DateTime(); $end->setTimestamp($eventCreated+$eventLength); $now = new DateTime(); $timeUntilEnd = $now->diff($end); $rowclass = 1 - $rowclass; ?> <?php
	if ($now < $end)
	{
		?>
  <td bgcolor="#000000"><?= $timeUntilEnd->h ?>
    h,
    <?= $timeUntilEnd->i ?>
    m,
    <?= $timeUntilEnd->s ?>
    s</td>
  <?php
	}
	else
	{
		?>
  <td bgcolor="#666666">Flag Expired</td>
  <?php

} ?>


<?php
} ?>
Name SteamID Reason Expires By When Time Left
<?= $row['player_nick'] ?> <?= $row['player_id'] ?> <?= $row['reason'] ?> <?= $end->format('m/d/Y g:ia') ?> <?= $row['admin_nick'] ?> <?= $created->format('m/d/Y g:ia') ?>
<?php mysqli_close($con); ?>
[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service