WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : mysql date/time converters
Categories : PHP, MySQL, Databases, Date Time Update Picture
Woody Stanford
Date : Feb 20th 2001
Grade : 5 of 5 (graded 1 times)
Viewed : 32554
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Woody Stanford
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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;

}

?>



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
formating MySQL Timestamp to get a propper Output. related : Timestamp, ereg_replace, mysql, substr
Categories : MySQL, PHP, Date Time, Databases
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
mysql date/time converters, part II
Categories : PHP, MySQL, Databases, Date Time
Simple function to return the number of days in a time span between 2 given dates.
Categories : PHP, Date Time, MySQL, Databases
Finding the day of the week for a specific date.
Categories : PHP, Databases, MySQL, Date Time
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 conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL
Checks Date-Input from HTML-Forms and converts to YYYY-MM-DD Format for MySQL Date-Fields
Categories : MySQL, Date Time, PHP, Databases
PHP Calendar Web App
Categories : PHP, Databases, MySQL, Date Time, Calendar
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
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