Title :
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
Rupali Gulande
Date :
Oct 24th 2007
Grade :
3 of 5 (graded 2 times)
Viewed :
21023
File :
4704.zip
Images :
No Images for this code example.
Search :
More code by Rupali Gulande
Action :
Grade This Code Example
Tools :
My Examples List
<?php
function mysqldate ( $dateformat , $mysqdate , $convert = true ) {
global $local_time ;
$m = $mysqdate ;
if ( empty( $m ) ) {
return false ;
}
$i = mktime (
(int) substr ( $m , 11 , 2 ), (int) substr ( $m , 14 , 2 ), (int) substr ( $m , 17 , 2 ),
(int) substr ( $m , 5 , 2 ), (int) substr ( $m , 8 , 2 ), (int) substr ( $m , 0 , 4 )
);
if( 'U' == $dateformat )
return $i ;
if ( - 1 == $i || false == $i )
$i = 0 ;
if ( !empty( $local_time -> month ) && !empty( $local_time -> weekday ) && $convert ) {
$datemonth = $local_time -> get_month ( date ( 'm' , $i ));
$datemonth_abbrev = $local_time -> get_month_abbrev ( $datemonth );
$dateweekday = $local_time -> get_weekday ( date ( 'w' , $i ));
$dateweekday_abbrev = $local_time -> get_weekday_abbrev ( $dateweekday );
$datemeridiem = $local_time -> get_meridiem ( date ( 'a' , $i ));
$datemeridiem_capital = $local_time -> get_meridiem ( date ( 'A' , $i ));
$dateformat = ' ' . $dateformat ;
$dateformat = preg_replace ( "/([^\\\])D/" , "\\1" . backslashit ( $dateweekday_abbrev ), $dateformat );
$dateformat = preg_replace ( "/([^\\\])F/" , "\\1" . backslashit ( $datemonth ), $dateformat );
$dateformat = preg_replace ( "/([^\\\])l/" , "\\1" . backslashit ( $dateweekday ), $dateformat );
$dateformat = preg_replace ( "/([^\\\])M/" , "\\1" . backslashit ( $datemonth_abbrev ), $dateformat );
$dateformat = preg_replace ( "/([^\\\])a/" , "\\1" . backslashit ( $datemeridiem ), $dateformat );
$dateformat = preg_replace ( "/([^\\\])A/" , "\\1" . backslashit ( $datemeridiem_capital ), $dateformat );
$dateformat = substr ( $dateformat , 1 , strlen ( $dateformat )- 1 );
}
$j = @ date ( $dateformat , $i );
if ( ! $j ) {
}
return $j ;
}
?>
Example Usage
<?php
$date1 = mysqldate ( 'l, d F Y ' , '2007-10-16 13:37:27' );
echo $date1 . " Mysql date format is 2007-10-16 13:37:27<br><br> " ;
?>
<?php
$date1 = mysqldate ( 'D, d F Y, - h : i : s ' , '2007-11-16 13:37:27' );
echo $date1 . " Mysql date format is 2007-11-16 13:37:27 " ;
?>
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 Phorum, MySQL, Language, UK date format, MySQL UK Date format Categories : PHP , Date Time , Strings , MySQL , Databases PHP Calendar Web App Categories : PHP , Databases , MySQL , Date Time , Calendar List people whose birthdays fall on the current Day and Month
Categories : Databases , Date Time , MySQL , PHP Data Retrieve from Mysql using AJAX with PHP Categories : PHP , AJAX , 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 mysql date/time converters Categories : PHP , MySQL , Databases , Date Time Finding the day of the week for a specific date.
Categories : PHP , Databases , MySQL , Date Time formating MySQL Timestamp to get a propper Output.
related : Timestamp, ereg_replace, mysql, substr Categories : MySQL , PHP , Date Time , Databases bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL , PHP , MySQL , Complete Programs , Databases 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 Specify your connection settings and create a link to a MySQL database. Categories : PHP , PHP Classes , Databases , MySQL , Beginner Guides
matthew waygood wrote : 1726
How about this instead?
<?php
// create unix timestamp from mysql datetime - careful of pre-1970
function timestamp_from_mysql ( $timestamp )
{
ereg ( "([0-9]{2,4})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,2})" , $timestamp , $regs );
ereg ( "([0-9]{2,4})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,2})" , $timestamp , $regs );
if ( sizeof ( $regs )> 1 )
{
if ( sizeof ( $regs )> 4 )
{
$date = mktime ( $regs [ 4 ], $regs [ 5 ], $regs [ 6 ], $regs [ 2 ], $regs [ 3 ], $regs [ 1 ]);
}
else
{
$date = mktime ( 0 , 0 , 0 , $regs [ 2 ], $regs [ 3 ], $regs [ 1 ]);
}
}
else
{
$date = 0 ;
}
return $date ;
}
// use the php date function to generate a date/time from a mysql datetime
// basically converts it to a unix datestamp, which date() uses.
// $format is the same as the one used by date(); so no need to recode if it is upgraded to include more parameters
function format_mysql_datetime ( $format , $mysql_datetime )
{
$unix = timestamp_from_mysql ( $mysql_datetime );
$date = " " ;
if( $unix > 0 )
{
$date = date ( $format , $unix );
}
return $date ;
}
?>