function getCreated() { if (!$this->localized) { $this->created->setTimeZone(new \DateTimeZone($this->timezone)); } return $this->created; }
public function rateThisBook() { $currentUserId = Auth::id(); $bookIdToRate = $_POST['bookIdToRate']; $ratingInput = ""; if (isset($_POST['userRating'])) { $ratingInput = $_POST['userRating']; } $datetime = new \DateTime(); $datetime->setTimeZone(new \DateTimeZone('Europe/Skopje')); $rated = DB::select('select * from bookstore.ratings where book_id = ' . $bookIdToRate . ' and user_id =' . $currentUserId); // print_r($rated); if (sizeof($rated) == 0) { if ($ratingInput >= 6 && $ratingInput <= 10) { $idTag = DB::table('ratings')->insertGetId(array('book_id' => $bookIdToRate, 'user_id' => $currentUserId, 'rating' => $ratingInput, 'created_at' => $datetime, 'updated_at' => $datetime)); } } else { if ($ratingInput >= 6 && $ratingInput <= 10) { DB::table('ratings')->where('book_id', $bookIdToRate)->where('user_id', $currentUserId)->update(array('rating' => $ratingInput, 'updated_at' => $datetime)); } } $path = '/book/' . $bookIdToRate; header("Location: " . $path); exit; }
/** * Transforms a date string in the configured timezone into a DateTime object * * @param string $value A value as produced by PHP's date() function * @return DateTime A DateTime object */ public function reverseTransform($value) { if (empty($value)) { return null; } if (!is_string($value)) { throw new UnexpectedTypeException($value, 'string'); } $outputTimezone = $this->outputTimezone; $inputTimezone = $this->inputTimezone; try { $dateTime = new \DateTime("$value $outputTimezone"); if ($inputTimezone != $outputTimezone) { $dateTime->setTimeZone(new \DateTimeZone($inputTimezone)); } return $dateTime; } catch (\Exception $e) { throw new \InvalidArgumentException('Expected a valid date string. ' . $e->getMessage(), 0, $e); } }
/** * * Renvoi le creneau actuel * * @return EdtCreneau EdtCreneau * */ public static function retrieveEdtCreneauActuel($v = 'now') { // we treat '' as NULL for temporal objects because DateTime('') == DateTime('now') // -- which is unexpected, to say the least. //$dt = new DateTime(); if ($v === null || $v === '') { $dt = null; } elseif ($v instanceof DateTime) { $dt = clone $v; } else { // some string/numeric value passed; we normalize that so that we can // validate it. try { if (is_numeric($v)) { // if it's a unix timestamp $dt = new DateTime('@'.$v, new DateTimeZone('UTC')); // We have to explicitly specify and then change the time zone because of a // DateTime bug: http://bugs.php.net/bug.php?id=43003 $dt->setTimeZone(new DateTimeZone(date_default_timezone_get())); } else { $dt = new DateTime($v); } } catch (Exception $x) { throw new PropelException('Error parsing date/time value: ' . var_export($v, true), $x); } } return EdtCreneauQuery::create()->filterByHeuredebutDefiniePeriode($dt->format("H:i:s"), Criteria::LESS_EQUAL) ->filterByHeurefinDefiniePeriode($dt->format("H:i:s"), Criteria::GREATER_THAN)->findOne(); }
/** * get a formatted date and time from a unix timestamp and time zone * * @param string $unixTime - a unix timestamp * @param string $timeZone - a timezone for this timestamp * * @return string $date - a formatted date and time with associated time zone */ function getTime($unixTime, $timeZone) { $date = new DateTime(); $date->setTimestamp($unixTime); $date->setTimeZone(new DateTimeZone($timeZone)); return $date->format("h:i:s a T D M j Y"); }
function convert($timestamp) { $schedule_date = new DateTime($timestamp, new DateTimeZone(date_default_timezone_get())); $schedule_date->setTimeZone(new DateTimeZone('UTC')); $triggerOn = $schedule_date->format('Y-m-d H:i:s'); echo $triggerOn; }
/** * This function does not usually need to be called directly. Instead, call * @{function:phabricator_date}, @{function:phabricator_time}, or * @{function:phabricator_datetime}. * * @param int Unix epoch timestamp. * @param PhabricatorUser User viewing the timestamp. * @param string Date format, as per DateTime class. * @return string Formatted, local date/time. */ function phabricator_format_local_time($epoch, $user, $format) { if (!$epoch) { // If we're missing date information for something, the DateTime class will // throw an exception when we try to construct an object. Since this is a // display function, just return an empty string. return ''; } $user_zone = $user->getTimezoneIdentifier(); static $zones = array(); if (empty($zones[$user_zone])) { $zones[$user_zone] = new DateTimeZone($user_zone); } $zone = $zones[$user_zone]; // NOTE: Although DateTime takes a second DateTimeZone parameter to its // constructor, it ignores it if the date string includes timezone // information. Further, it treats epoch timestamps ("@946684800") as having // a UTC timezone. Set the timezone explicitly after constructing the object. try { $date = new DateTime('@' . $epoch); } catch (Exception $ex) { // NOTE: DateTime throws an empty exception if the format is invalid, // just replace it with a useful one. throw new Exception(pht("Construction of a DateTime() with epoch '%s' " . "raised an exception.", $epoch)); } $date->setTimeZone($zone); return PhutilTranslator::getInstance()->translateDate($format, $date); }
public static function convert_time($time, $local_timezone, $target_timezone) { date_default_timezone_set($local_timezone); $datetime = new DateTime($time); $timezone = new DateTimeZone($target_timezone); return $datetime->setTimeZone($timezone)->format('Y-m-d H:i'); }
/** * Sets the value of [timestamp] column to a normalized version of the date/time value specified. * * @param mixed $v string, integer (timestamp), or DateTime value. Empty string will * be treated as NULL for temporal objects. */ public function setTimeStamp($v) { // we treat '' as NULL for temporal objects because DateTime('') == DateTime('now') // -- which is unexpected, to say the least. if ($v === null || $v === '') { $dt = null; } elseif ($v instanceof DateTime) { $dt = $v; } else { // some string/numeric value passed; we normalize that so that we can // validate it. try { if (is_numeric($v)) { // if it's a unix timestamp $dt = new DateTime('@' . $v, new DateTimeZone('UTC')); // We have to explicitly specify and then change the time zone because of a // DateTime bug: http://bugs.php.net/bug.php?id=43003 $dt->setTimeZone(new DateTimeZone(date_default_timezone_get())); } else { $dt = new DateTime($v); } } catch (Exception $x) { throw new PropelException('Error parsing date/time value: ' . var_export($v, true), $x); } } if ($this->timeStamp !== null || $dt !== null) { // (nested ifs are a little easier to read in this case) $currNorm = $this->timeStamp !== null && ($tmpDt = new DateTime($this->timeStamp)) ? $tmpDt->format('Y-m-d H:i:s') : null; $newNorm = $dt !== null ? $dt->format('Y-m-d H:i:s') : null; if ($currNorm !== $newNorm) { $this->timeStamp = $dt ? $dt->format('Y-m-d H:i:s') : null; } } // if either are not null }
function phabricator_datetime($epoch, $user) { $zone = new DateTimeZone($user->getTimezoneIdentifier()); $date = new DateTime('@' . $epoch); $date->setTimeZone($zone); return $date->format('M j Y, g:i A'); }
public function getCodeByDateReuse($subcode = null) { $stmt = $this->getPdo()->prepare("\n\t\t\t\tSELECT DATE_FORMAT(CURRENT_TIMESTAMP, '%Y-%m-%dT%TZ')\n\t\t\t"); $stmt->execute(); $date = new \DateTime($stmt->fetchColumn()); $date->setTimeZone(new \DateTimeZone($this->config['timezone'])); return $this->getCode($subcode, $date->format('Ymd')); }
public function testTransform_differentTimezones() { $transformer = new StringToDateTimeTransformer(array('input_timezone' => 'America/New_York', 'output_timezone' => 'Asia/Hong_Kong')); $output = new \DateTime('2010-02-03 04:05:06 America/New_York'); $input = $output->format('Y-m-d H:i:s'); $output->setTimeZone(new \DateTimeZone('Asia/Hong_Kong')); $this->assertDateTimeEquals($output, $transformer->transform($input)); }
/** * * @param <type> $gmdate * @param <type> $format * @param <type> $timezone * @return <type> */ public static function tzFormatDate($gmdate, $format = false, $timezone = false) { $format = $format ?: DATE_FORMAT; $timezone = $timezone ?: 'UTC'; $cnv_date = new DateTime($gmdate); $cnv_date->setTimeZone(new DateTimeZone($timezone)); return $cnv_date->format($format); }
/** * @expectedException InvalidArgumentException */ function testSetDateTimeInvalid() { $tz = new \DateTimeZone('Europe/Amsterdam'); $dt = new \DateTime('1985-07-04 01:30:00', $tz); $dt->setTimeZone($tz); $elem = new MultiDateTime('DTSTART'); $elem->setDateTimes(array($dt), 7); }
public function getNTPTime() { // go and get the time from some NTP server (for now, we doing the same as the default, as an example) $timezone = new \DateTimeZone($this->timezone); $date = new \DateTime(); $date->setTimeZone($timezone); return $date; }
public function testReverseTransform_differentTimezones() { $reverseTransformer = new DateTimeToStringTransformer('America/New_York', 'Asia/Hong_Kong', 'Y-m-d H:i:s'); $output = new \DateTime('2010-02-03 04:05:06 Asia/Hong_Kong'); $input = $output->format('Y-m-d H:i:s'); $output->setTimeZone(new \DateTimeZone('America/New_York')); $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input)); }
public function format($value) { if ($value === null) { return $this->nullValue; } $dateTime = new DateTime($value, new DateTimeZone($this->fromTimezone)); $dateTime->setTimeZone(new DateTimeZone($this->toTimezone)); return $dateTime->format($this->dateTimeFormat); }
function testGetDateTimeCached() { $tz = new DateTimeZone('Europe/Amsterdam'); $dt = new DateTime('1985-07-04 01:30:00', $tz); $dt->setTimeZone($tz); $elem = new Sabre_VObject_Element_DateTime('DTSTART'); $elem->setDateTime($dt); $this->assertEquals($elem->getDateTime(), $dt); }
function formatted_time($datetime_str = 'now', $timestamp_format = NULL, $timezone = NULL) { $tz = new DateTimeZone($timezone ? $timezone : date_default_timezone_get()); $time = new DateTime($datetime_str, $tz); if ($time->getTimeZone()->getName() !== $tz->getName()) { $time->setTimeZone($tz); } return $time->format($timestamp_format); }
public function fromTimestampToDateTime($timestamp) { try { $dateTime = new \DateTime(); $dateTime->setTimeZone($this->timeZone)->setTimestamp($timestamp); return $dateTime; } catch (\Exception $exception) { throw new DateTimeException('There was an exception while creating a \\DateTime object.', $exception); } }
private function getDateEpochForTimeZone($epoch, $src_zone, $format, $adjust, $dst_zone) { $src = new DateTime('@' . $epoch); $src->setTimeZone($src_zone); if (strlen($adjust)) { $adjust = ' ' . $adjust; } $dst = new DateTime($src->format($format) . $adjust, $dst_zone); return $dst->format('U'); }
public function getInterface() { if (is_null(static::$pdo)) { static::$pdo = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';', DB_USER, DB_PASSWORD); $now = new DateTime(); $now->setTimeZone(new DateTimeZone(TIME_ZONE)); static::$pdo->exec("SET time_zone='" . $now->format('P') . "'"); } return static::$pdo; }
/** * Given a date/time in UTC and a target timezone, yields a DateTime object converted from * UTC to the target timezone. * @param mixed $datetime \DateTime instance or String. * @param mized $targetTimeZone \DateTimeZone instance or String. * @return \DateTime */ public static function convertUtcDateTimeToTimeZone($datetime, $targetTimeZone) { if (!$datetime instanceof \DateTime) { $datetime = new \DateTime($datetime, new \DateTimeZone("UTC")); } if (!$targetTimeZone instanceof \DateTimeZone) { $targetTimeZone = new \DateTimeZone($targetTimeZone); } $datetime->setTimeZone($targetTimeZone); return $datetime; }
public function rememberMe($data = null) { $this->set($data); $t = new DateTime(); $t->modify('+2 weeks'); $t->setTimeZone(new DateTimeZone('UTC')); $token_expires_at = $t->format('D M d H:i:s e Y'); $key = "{$this->data['Member']['username']}--{$token_expires_at}"; $this->data['Member']['remember_token_expires_at'] = $t->format('Y-m-d H:i:s'); $this->data['Member']['remember_token'] = Member::encrypt($key, $this->data['Member']['salt']); return $this->save(false); }
private function convertTime($time) { if ($this->isDateTime($time)) { $time->setTimeZone(new \DateTimeZone('GMT')); return $time; } $time = (int) $time; $dateTime = new \DateTime(); $dateTime->setTimestamp($time); $dateTime->setTimeZone(new \DateTimeZone('GMT')); return $dateTime; }
/** * Used for GMT offset for a timezone * @param string $timezone: timezone name * @return string GMT offset for the given timezone * @author Laxmi Saini */ function getTimezoneOffset($timezone = null) { if ($timezone != '') { $dateTimeObj = new DateTime(); $date_time_zone = CakeTime::timezone($timezone); $dateTimeObj->setTimeZone($date_time_zone); $offset = $dateTimeObj->getOffset(); return $this->formatGmtOffset($offset); } else { return ""; } }
function datetime_convert($from = 'UTC', $to = 'UTC', $s = 'now', $fmt = "Y-m-d H:i:s") { // Slight hackish adjustment so that 'zero' datetime actually returns what is intended // otherwise we end up with -0001-11-30 ... // add 32 days so that we at least get year 00, and then hack around the fact that // months and days always start with 1. if (substr($s, 0, 10) == '0000-00-00') { $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC')); return str_replace('1', '0', $d->format($fmt)); } $d = new DateTime($s, new DateTimeZone($from)); $d->setTimeZone(new DateTimeZone($to)); return $d->format($fmt); }
/** * Retrourne la periode actuelle, ou null si aucune periode n'est trouve pour le jours actuel * * @param mixed $v string, integer (timestamp), or DateTime value. Empty string will * be treated as NULL for temporal objects. * @return EdtCalendrierPeriode la periode actuelle */ public static function retrieveEdtCalendrierPeriodeActuelle($v = 'now') { // we treat '' as NULL for temporal objects because DateTime('') == DateTime('now') // -- which is unexpected, to say the least. //$dt = new DateTime(); if ($v === null || $v === '') { $dt = null; } elseif ($v instanceof DateTime) { $dt = clone $v; } else { // some string/numeric value passed; we normalize that so that we can // validate it. try { if (is_numeric($v)) { // if it's a unix timestamp $dt = new DateTime('@'.$v, new DateTimeZone('UTC')); // We have to explicitly specify and then change the time zone because of a // DateTime bug: http://bugs.php.net/bug.php?id=43003 $dt->setTimeZone(new DateTimeZone(date_default_timezone_get())); } else { $dt = new DateTime($v); } } catch (Exception $x) { throw new PropelException('Error parsing date/time value: ' . var_export($v, true), $x); } } $edt_periode_actuelle=Null; $intervalle_periode=Null; foreach (EdtCalendrierPeriodePeer::retrieveAllEdtCalendrierPeriodesOrderByTime() as $edtPeriode) { if ($edtPeriode->getJourdebutCalendrier('Y-m-d') <= $dt->format('Y-m-d') && $edtPeriode->getJourfinCalendrier('Y-m-d') >= $dt->format('Y-m-d')) { if (is_null($edt_periode_actuelle)){ //c'est la première periode rencontrée qui correspond $edt_periode_actuelle=$edtPeriode; $intervalle_periode=$edtPeriode->getFinCalendrierTs()-$edtPeriode->getDebutCalendrierTs(); }else{ //si une periode plus courte correspond on prend celle là if ($edtPeriode->getFinCalendrierTs()-$edtPeriode->getDebutCalendrierTs()<=$intervalle_periode) { $edt_periode_actuelle = $edtPeriode; $intervalle_periode=$edtPeriode->getFinCalendrierTs()-$edtPeriode->getDebutCalendrierTs(); } } } } return $edt_periode_actuelle; //return null; // return EdtCalendrierPeriodeQuery::create() // ->filterByJourdebutCalendrier($dt, Criteria::LESS_EQUAL) // //->filterByHeuredebutCalendrier($dt, Criteria::GREATER_EQUAL) // ->filterByJourfinCalendrier($dt, Criteria::GREATER_EQUAL) // //->filterByHeurefinCalendrier($dt, Criteria::LESS_EQUAL) // ->findOne(); }
/** * Transforms a date string in the configured timezone into a DateTime object * * @param string $value A value as produced by PHP's date() function * @return DateTime A DateTime object */ public function transform($value) { $inputTimezone = $this->getOption('input_timezone'); $outputTimezone = $this->getOption('output_timezone'); try { $dateTime = new \DateTime("{$value} {$inputTimezone}"); if ($inputTimezone != $outputTimezone) { $dateTime->setTimeZone(new \DateTimeZone($outputTimezone)); } return $dateTime; } catch (\Exception $e) { throw new \InvalidArgumentException('Expected a valid date string. ' . $e->getMessage(), 0, $e); } }
public function runs($cronTime, $date) { $tz = new \DateTimeZone('Australia/Melbourne'); if (is_int($date)) { $date = new \DateTime('@' . $date); $date->setTimeZone($tz); } else { $date = \DateTime::createFromFormat('Y-m-d H:i', $date, $tz); } $jobString = $cronTime . ' JOB'; $parser = new Parser(new \DateTimeZone('Australia/Melbourne')); $jobs = $parser->parse($jobString, false); $runs = $jobs[0]->runsAt($date); return $runs; }