|
|
|
Just some real-life functions you can call to perform common date/time conversions between
MySQL datetime format, MySQL timestamp format and UNIX timestamp (i.e. seconds after
epoch) with "human" output.
<?
//we use UNIX's time specification as the base specification
function mysql_datetime_to_human($dt)
{
$yr=strval(substr($dt,0,4));
$mo=strval(substr($dt,5,2));
$da=strval(substr($dt,8,2));
$hr=strval(substr($dt,11,2));
$mi=strval(substr($dt,14,2));
// $se=strval(substr($dt,17,2));
return date("M/d/Y H:i", mktime ($hr,$mi,0,$mo,$da,$yr))." MST";
}
function mysql_timestamp_to_human($dt)
{
$yr=strval(substr($dt,0,4));
$mo=strval(substr($dt,4,2));
$da=strval(substr($dt,6,2));
$hr=strval(substr($dt,8,2));
$mi=strval(substr($dt,10,2));
//$se=strval(substr($dt,12,2));
return date("M/d/Y H:i", mktime ($hr,$mi,0,$mo,$da,$yr))." MST";
}
function mysql_timestamp_to_timestamp($dt)
{
$yr=strval(substr($dt,0,4));
$mo=strval(substr($dt,4,2));
$da=strval(substr($dt,6,2));
$hr=strval(substr($dt,8,2));
$mi=strval(substr($dt,10,2));
$se=strval(substr($dt,10,2));
return mktime($hr,$mi,$se,$mo,$da,$yr);
}
function mysql_datetime_to_timestamp($dt)
{
$yr=strval(substr($dt,0,4));
$mo=strval(substr($dt,5,2));
$da=strval(substr($dt,8,2));
$hr=strval(substr($dt,11,2));
$mi=strval(substr($dt,14,2));
$se=strval(substr($dt,17,2));
return mktime($hr,$mi,$se,$mo,$da,$yr);
}
function timestamp_to_mysql($ts)
{
$d=getdate($ts);
$yr=$d["year"];
$mo=$d["mon"];
$da=$d["mday"];
$hr=$d["hours"];
$mi=$d["minutes"];
$se=$d["seconds"];
return sprintf("%04d%02d%02d%02d%02d%02d",$yr,$mo,$da,$hr,$mi,$se);
}
function timeleft($begin,$end)
{
//for two timestamp format dates, returns the plain english difference between them.
//note these dates are UNIX timestamps
$dif=$end-$begin;
$years=intval($dif/(60*60*24*365));
$dif=$dif-($years*(60*60*24*365));
$months=intval($dif/(60*60*24*30));
$dif=$dif-($months*(60*60*24*30));
$weeks=intval($dif/(60*60*24*7));
$dif=$dif-($weeks*(60*60*24*7));
$days=intval($dif/(60*60*24));
$dif=$dif-($days*(60*60*24));
$hours=intval($dif/(60*60));
$dif=$dif-($hours*(60*60));
$minutes=intval($dif/(60));
$seconds=$dif-($minutes*60);
$s="";
//if ($years<>0) $s.= $years." years ";
//if ($months<>0) $s.= $months." months ";
if ($weeks<>0) $s.= $weeks." weeks ";
if ($days<>0) $s.= $days." days ";
if ($hours<>0) $s.= $hours." hours ";
if ($minutes<>0) $s.= $minutes." minutes ";
//if ($seconds<>0) $s.= $seconds." seconds ";
return $s;
}
?> |
|
| Finding the day of the week for a specific date.
Categories : PHP, Databases, MySQL, Date Time | | | mysql date/time converters, part II Categories : PHP, MySQL, Databases, Date Time | | | Data Retrieve from Mysql using AJAX with PHP Categories : PHP, AJAX, Date Time, Databases, MySQL | | | Phorum, MySQL, Language, UK date format, MySQL UK Date format Categories : PHP, Date Time, Strings, MySQL, Databases | | | List people whose birthdays fall on the current Day and Month
Categories : Databases, Date Time, MySQL, PHP | | | In Mysql, the 'datetime' data type is used to store date that looks like '2007-10-16 16:26:30'. we are going to design a function called mysqldate() that takes a mysql date and output format as input and outputs a well formated date. Categories : PHP, Date Time, Databases, MySQL | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | Simple function to return the number of days in a time span between 2 given dates. Categories : PHP, Date Time, MySQL, Databases | | | Checks Date-Input from HTML-Forms and converts to YYYY-MM-DD Format for MySQL Date-Fields Categories : MySQL, Date Time, PHP, Databases | | | Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL | | | Count how many weeks in the month have a specified day, such as Mon, Tue, etc. Var avail - number of days - first day name of the month, occurrences of Sun, occurrences of Mon, etc. Allows you to calculate number of working hours exclude Holidays. Categories : Calendar, Date Time, PHP, Databases, MySQL | | | How to Insert a Date Format Into MySQL from PHP Categories : PHP, Databases, MySQL, Date Time, Beginner Guides | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | formating MySQL Timestamp to get a propper Output.
related : Timestamp, ereg_replace, mysql, substr Categories : MySQL, PHP, Date Time, Databases | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | |
|
|
|