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
Share on other sites

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();

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.