Showing posts with label date format. Show all posts
Showing posts with label date format. Show all posts

Thursday, November 10, 2016

Php determine if date string is a valid date in that format

Code snippet

function validateDate($date_string)
{
    $date_value = DateTime::createFromFormat('Y-m-d', $date_string);
    return $date_value && $date_value->format('Y-m-d') === $date_string;
}

Some examples

var_dump( validateDate('2016-13-10'));   // false
var_dump( validateDate('20160-13-10'));  // false
var_dump( validateDate('2016-11-32'));   // false
var_dump( validateDate('2017-02-29'));   // false

var_dump( validateDate('2016-11-02'));   // true
var_dump( validateDate('1970-12-21'));   // true
var_dump( validateDate('2017-07-29'));   // true

Online fiddle link


Tuesday, June 3, 2014

MySql Convert String to Date

STR_TO_DATE('Jun/03/2014', '%M/%d/%Y');  => 2014-06-03; [VALID]
STR_TO_DATE('2014-04-25', '%Y-%m-%d'); => 2014-06-25; [VALID]
STR_TO_DATE( '69/Jun/2014', '%d/%M/%Y' ) => NULL; [INVALID]
 
The formats that can be used are:
Format Description
%aAbbreviated weekday name
%bAbbreviated month name
%cMonth, numeric
%DDay of month with English suffix
%dDay of month, numeric (00-31)
%eDay of month, numeric (0-31)
%fMicroseconds
%HHour (00-23)
%hHour (01-12)
%IHour (01-12)
%iMinutes, numeric (00-59)
%jDay of year (001-366)
%kHour (0-23)
%lHour (1-12)
%MMonth name
%mMonth, numeric (00-12)
%pAM or PM
%rTime, 12-hour (hh:mm:ss AM or PM)
%SSeconds (00-59)
%sSeconds (00-59)
%TTime, 24-hour (hh:mm:ss)
%UWeek (00-53) where Sunday is the first day of week
%uWeek (00-53) where Monday is the first day of week
%VWeek (01-53) where Sunday is the first day of week, used with %X
%vWeek (01-53) where Monday is the first day of week, used with %x
%WWeekday name
%wDay of the week (0=Sunday, 6=Saturday)
%XYear of the week where Sunday is the first day of week, four digits, used with %V
%xYear of the week where Monday is the first day of week, four digits, used with %v
%YYear, four digits
%yYear, two digits
 

Wednesday, December 4, 2013

mysql date format

The DATE_FORMAT() function is used to display date/time data in different formats.

Syntax

DATE_FORMAT(date,format)
Where date is a valid date and format specifies the output format for the date/time.
The formats that can be used are from http://www.w3schools.com/sql/func_date_format.asp:

Format Description
%aAbbreviated weekday name
%bAbbreviated month name
%cMonth, numeric
%DDay of month with English suffix
%dDay of month, numeric (00-31)
%eDay of month, numeric (0-31)
%fMicroseconds
%HHour (00-23)
%hHour (01-12)
%IHour (01-12)
%iMinutes, numeric (00-59)
%jDay of year (001-366)
%kHour (0-23)
%lHour (1-12)
%MMonth name
%mMonth, numeric (00-12)
%pAM or PM
%rTime, 12-hour (hh:mm:ss AM or PM)
%SSeconds (00-59)
%sSeconds (00-59)
%TTime, 24-hour (hh:mm:ss)
%UWeek (00-53) where Sunday is the first day of week
%uWeek (00-53) where Monday is the first day of week
%VWeek (01-53) where Sunday is the first day of week, used with %X
%vWeek (01-53) where Monday is the first day of week, used with %x
%WWeekday name
%wDay of the week (0=Sunday, 6=Saturday)
%XYear of the week where Sunday is the first day of week, four digits, used with %V
%xYear of the week where Monday is the first day of week, four digits, used with %v
%YYear, four digits
%yYear, two digits

Examples in fiddle:

http://sqlfiddle.com/#!2/ccc08/3