Jump to content

PHP Count down and Time Zones


iconicCreator

Recommended Posts

 

I am in a Central Time Zone, I was searching for a count down script that shows the number of days left to an event.

I stumbled upon this script which uses several function to display the messages.

 

However, the time is about an hour behind or at least not Central Time Zone.

 

The time is the same on my local and remote servers, so I conclude it has to do with the script.

 

I am not sure how to resolved this.

 

Here it is:

 

<?php
class event{
var $today;
var $eventDate;

function event($d,$m,$y){
	$this->eventDate = mktime(0,0,0,$m,$d,$y);
	$this->today = date("d-m-y");

}

function daysLeft(){
	$daysUntilEvent = date('z',$this->eventDate);
	$currentDay=date('z');
	$daysRemain = $daysUntilEvent - $currentDay;
return $daysRemain; 
}

function eventOver(){
	if($this->daysLeft() <=0 ){
		return 'Next Event is July 10th.';
	}else{
	 	return 'Event Begins in <span class="daysToEvent">'.$this->daysLeft().'</span> Days!';
	}
}

}

$event = new event(24,06,2011);

echo $event->eventOver();
?>

 

Thanks everyone!

 

IC

Link to comment
https://forums.phpfreaks.com/topic/238115-php-count-down-and-time-zones/
Share on other sites

same link i posted...lol...just realized my sentence got all jumbled..weird

 

Thank you, I think I had looked at that link before but I shall play around with it and see what happens.

 

Thanks again.

Here's how I'd do it with >=PHP5.3 and OOP

 

<?php

class event {

private $_eventDate, $_datetime, $_timezone = 'America/Vancouver';

public function __construct( $d,$m,$y ) {
	$date = $y.'-'.$m.'-'.$d.' 23:59:59';
	$this->_datetime = new DateTime( $date, new DateTimeZone($this->_timezone) );
}

private function daysLeft() {
	$now = new DateTime( NULL, new DateTimeZone($this->_timezone) );
	return floor(($this->_datetime->getTimestamp() - $now->getTimestamp()) / 86400);
}

public function eventOver() {
	$daysLeft = $this->daysLeft();
	if($daysLeft <= 0 ){
		return 'Next Event is July 10th.';
	}else{
	 	return 'Event Begins in <span class="daysToEvent">'.$daysLeft.'</span> Days!';
	}
}

}

$obj = new event( 10,10,2011 );

echo $obj->eventOver();

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.