if (!function_exists('utc_datetime')) {
function utc_datetime($datetime = '', $format = 'Y-m-d H:i:s') {
$time = time();
if ($datetime !== '') {
$time = strtotime($datetime);
}
$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$date->setTimestamp($time);
return $date->format($format);
}
}
if (!function_exists('local_datetime')) {
function local_datetime($datetime = '', $timezone = 'Asia/Ulaanbaatar', $format = 'Y-m-d H:i:s') {
$time = time();
if ($datetime !== '') {
$time = strtotime($datetime);
}
$date = new DateTime();
$date->setTimezone(new DateTimeZone($timezone));
$date->setTimestamp($time);
return $date->format($format);
}
}
?>
USAGE
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class boroo extends BaseController {
function __construct() {
parent::__construct();
}
public function hello() {
global $RTR;
$expire = (7 * 24 * 60 * 60);
$this->input->set_cookie("time", time(), $expire);
$time = $this->input->cookie('time');
$timezone = $this->getZone();
$format = 'Y-m-d H:i:s';
$this->load->helper('date');
date_default_timezone_set('Asia/Ulaanbaatar');
$time = strtotime(utc_datetime('2013-09-10 10:30:45'));
$newtime = mktime(date('H',$time), date('i',$time), date('s',$time));
$date = date($format, $newtime);
echo $date;
exit;
echo local_datetime('2013-09-10 10:30:45') . "=>" . utc_datetime('2013-09-10 10:30:45');
exit;
$data['message'] = 'Date is ' . $dateStr . ', IP is ' . $this->getIp();
$this->load->view($RTR->fetch_directory() . $RTR->fetch_class() . '/hello', $data);
}
}
or
$date = new DateTime('2013-09-10 10:30:45');
$dateStr = date("Y-m-d H:i:s", strtotime('+5 minutes', $date->getTimestamp()));
echo $dateStr;
exit;
No comments:
Post a Comment