0
<?php
date_default_timezone_set('America/New_York');
$current_time = time();
$now = new DateTime();
$b = $current_time;
$future_date = new DateTime('2011-05-11 09:30:00');
$interval = $future_date->diff($now);
echo $interval->format("%h hours, %i minutes, %s seconds");
?>

i want to show the the Market remaining time, Market open at 9:30 am, the above code is working fine but problem is $current_time is showing my system current time instead of Nerw york time, so its showing the remaining time by by considering my system time.i want to show the American current time so i can easily show the remaining time. Thank in Advance.

Nadeem
  • 261
  • 1
  • 4
  • 19
  • Read [this](http://stackoverflow.com/questions/38327793/how-can-i-insert-real-time-of-an-event-into-database-using-php-mysqli) I hope it will be useful for you – Ayaz Ali Shah Sep 28 '16 at 08:59
  • Is the above just an example? America observes DST so it'll break for half the year if you use a fixed date in May for the time. – bcmcfc Sep 28 '16 at 09:06
  • There is more wrong with that code than you think – RiggsFolly Sep 28 '16 at 09:06

1 Answers1

0

This I think is closer to what you need.

I have made sure that I set a specific timezone on both the DateTime objects so there can be no confusion

This also takes into account the possibility that the market could already be open, and tells you how long to closing time.

It also make use of Todays date in all cases, so any daylight saving time should be coped with.

There is also a time setter for the current time so you can check what happens at different times of the day, see $now->setTime(10, 30, 0); for setting a specific time of day.

<?php

$now = new DateTime();
$now->setTimezone(new DateTimeZone('America/New_York'));

// To test what happens at different times of the current day
// Set a specific time for the NOW DateTIme
$now->setTime(10, 30, 0);

echo 'Now = ' . $now->format('d/m/Y H:i:s').PHP_EOL;

$opens = new DateTime();
$opens->setTimezone(new DateTimeZone('America/New_York'));
$opens->setTime(9, 30, 0);
echo 'Opens = ' . $opens->format('d/m/Y H:i:s').PHP_EOL;

$interval = $now->diff($opens);

// if invert == 1 its a minus difference so market is open
if ( $interval->invert == 1){
    // Its already open
    echo $interval->format("Market has been open for: %h hours, %i minutes, %s seconds").PHP_EOL;

    // When will it close
    $close = new DateTime();
    $close->setTimezone(new DateTimeZone('America/New_York'));
    $close->setTime(3, 30, 0);
    $interval = $now->diff($close);
    echo $interval->format("Market closes in: %h hours, %i minutes, %s seconds").PHP_EOL;
} else {
    // it will open in
    echo $interval->format("Market opens in: %h hours, %i minutes, %s seconds").PHP_EOL;
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149