<?php
/*
* Copyright 2008 Blandware (http://www.blandware.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This file contains functions which allow to work with dates and time.
*
* @package AtleapLite
* @author Roman Puchkovskiy
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/**
* Makes a timestamp from a string.
*
* @param string $string string that represents some date/time
*/
function makeTimestamp($string)
{
if(empty($string)) {
$string = "now";
}
$time = strtotime($string);
if (is_numeric($time) && $time != -1)
return $time;
// is mysql timestamp format of YYYYMMDDHHMMSS?
if (preg_match('/^\d{14}$/', $string)) {
$time = mktime(substr($string,8,2),substr($string,10,2),substr($string,12,2),
substr($string,4,2),substr($string,6,2),substr($string,0,4));
return $time;
}
// couldn't recognize it, try to return a time
$time = (int) $string;
if ($time > 0)
return $time;
else
return time();
}
/**
* Formats a date according to a given format.
*
* @param int $value timestamp to format
* @param string $pattern pattern to use when formatting (as date()
* understands)
* @return string formatted date
*/
function formatDate($value, $pattern) {
return date($pattern, $value);
}
/**
* Parses a date from a string.
*
* @param string $value string to parse
* @param strig $pattern pattern
* @return int timestamp
* @todo this implementation is extremely stupid :( need to parse it correctly
*/
function parseDate($value, $pattern) {
if ($pattern == "d.m.Y") {
list($d, $m, $y) = explode('.', $value);
} else {
list($y, $m, $d) = explode('-', $value);
}
return mktime(0, 0, 0, $m, $d, $y);
}
/**
* Converts a date to format understandable by DB.
*
* @param string $value string representing date
* @return string converted date
*/
function dateToDbFormat($value) {
$value = parseDate($value, DATE_PATTERN);
return formatDate($value, "Y-m-d");
}
/**
* Converts a date from format understandable by DB.
*
* @param string $value string representing date in DB format
* @return string converted date
*/
function dateFromDbFormat($value) {
$value = parseDate($value, "Y-m-d");
return formatDate($value, DATE_PATTERN);
}
/**
* Returns the current date formatted for database.
*
* @return string formatted date
*/
function getNowDate() {
return date('Y-m-d H:i:s');
}
/**
* Determines whether the current date/time is between two dates which are
* given using the database representation.
*
* @param string $begin begin date
* @param string $end end date
* @return true if between (inclusive)
*/
function isNowBetween($begin, $end) {
$begin = strtotime($begin);
$end = strtotime($end);
$now = time();
return $now >= $begin && $now <= $end;
}
?>