Is there a way to check to see if a date/time is valid you would think these would be easy to check:
$date = '0000-00-00';
$time = '00:00:00';
$dateTime = $date . ' ' . $time;
if(strtotime($dateTime)) {
// why is this valid?
}
what really gets me is this:
echo date('Y-m-d', strtotime($date));
results in: "1999-11-30",
huh? i went from 0000-00-00 to 1999-11-30 ???
I know i could do comparison to see if the date is either of those values is equal to the date i have but it isn't a very robust way to check. Is there a good way to check to see if i have a valid date? Anyone have a good function to check this?
Edit:
People are asking what i'm running:
Running PHP 5.2.5 (cli) (built: Jul 23 2008 11:32:27) on Linux localhost 2.6.18-53.1.14.el5 #1 SMP Wed Mar 5 11:36:49 EST 2008 i686 i686 i386 GNU/Linux
From php.net
<?php
function isValidDateTime($dateTime)
{
if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $dateTime, $matches)) {
if (checkdate($matches[2], $matches[3], $matches[1])) {
return true;
}
}
return false;
}
?>
As mentioned here: https://bugs.php.net/bug.php?id=45647
There is no bug here, 00-00-00 means 2000-00-00, which is 1999-12-00,
which is 1999-11-30. No bug, perfectly normal.
And as shown with a few tests, rolling backwards is expected behavior, if a little unsettling:
>> date('Y-m-d', strtotime('2012-03-00'))
string: '2012-02-29'
>> date('Y-m-d', strtotime('2012-02-00'))
string: '2012-01-31'
>> date('Y-m-d', strtotime('2012-01-00'))
string: '2011-12-31'
>> date('Y-m-d', strtotime('2012-00-00'))
string: '2011-11-30'
echo date('Y-m-d', strtotime($date));
results in: "1999-11-30"
The result of strtotime is 943920000 - this is the number of seconds, roughly, between the Unix epoch (base from which time is measured) to 1999-11-30.
There is a documented mysql bug on mktime(), localtime(), strtotime() all returning this odd value when you try a pre-epoch time (including "0000-00-00 00:00:00"). There's some debate on the linked thread as to whether this is actually a bug:
Since the time stamp is started from 1970, I don't think it supposed to
work in anyways.
Below is a function that I use for converting dateTimes such as the above to a timestamp for comparisons, etc, which may be of some use to you, for dates beyond "0000-00-00 00:00:00"
/**
* Converts strings of the format "YYYY-MM-DD HH:MM:SS" into php dates
*/
function convert_date_string($date_string)
{
list($date, $time) = explode(" ", $date_string);
list($hours, $minutes, $seconds) = explode(":", $time);
list($year, $month, $day) = explode("-", $date);
return mktime($hours, $minutes, $seconds, $month, $day, $year);
}
Don't expect coherent results when you're out of range:
cf strtotime
cf Gnu Calendar-date-items.html
"For numeric months, the ISO 8601
format ‘year-month-day’ is allowed,
where year is any positive number,
month is a number between 01
and 12, and day is a
number between 01 and 31. A
leading zero must be present if a
number is less than ten."
So '0000-00-00' gives weird results, that's logical!
"Additionally, not all
platforms support negative timestamps,
therefore your date range may be
limited to no earlier than the Unix
epoch. This means that e.g.
%e, %T, %R and %D (there might be
more) and dates prior to Jan
1, 1970 will not work on Windows, some
Linux distributions, and a few other
operating systems."
cf strftime
Use checkdate function instead (more robust):
month:
The month is between 1 and 12 inclusive.
day:
The day is within the allowed number of days for the given
month. Leap year s are taken
into consideration.
year:
The year is between 1 and 32767 inclusive.
This version allows for the field to be empty, has dates in mm/dd/yy or mm/dd/yyyy format, allow for single digit hours, adds optional am/pm, and corrects some subtle flaws in the time match.
Still allows some pathological times like '23:14 AM'.
function isValidDateTime($dateTime) {
if (trim($dateTime) == '') {
return true;
}
if (preg_match('/^(\d{1,2})\/(\d{1,2})\/(\d{2,4})(\s+(([01]?[0-9])|(2[0-3]))(:[0-5][0-9]){0,2}(\s+(am|pm))?)?$/i', $dateTime, $matches)) {
list($all,$mm,$dd,$year) = $matches;
if ($year <= 99) {
$year += 2000;
}
return checkdate($mm, $dd, $year);
}
return false;
}
If you just want to handle a date conversion without the time for a mysql date field, you can modify this great code as I did.
On my version of PHP without performing this function I get "0000-00-00" every time. Annoying.
function ConvertDateString ($DateString)
{
list($year, $month, $day) = explode("-", $DateString);
return date ("Y-m-d, mktime (0, 0, 0, $month, $day, $year));
}
<?php
function is_valid_date($user_date=false, $valid_date = "1900-01-01") {
$user_date = date("Y-m-d H:i:s",strtotime($user_date));
return strtotime($user_date) >= strtotime($valid_date) ? true : false;
}
echo is_valid_date("00-00-00") ? 1 : 0; // return 0
echo is_valid_date("3/5/2011") ? 1 : 0; // return 1
I have been just changing the martin answer above, which will validate any type of date and return in the format you like.
Just change the format by editing below line of script
strftime("10-10-2012", strtotime($dt));
<?php
echo is_date("13/04/10");
function is_date( $str ) {
$flag = strpos($str, '/');
if(intval($flag)<=0){
$stamp = strtotime( $str );
} else {
list($d, $m, $y) = explode('/', $str);
$stamp = strtotime("$d-$m-$y");
}
//var_dump($stamp) ;
if (!is_numeric($stamp)) {
//echo "ho" ;
return "not a date" ;
}
$month = date( 'n', $stamp ); // use n to get date in correct format
$day = date( 'd', $stamp );
$year = date( 'Y', $stamp );
if (checkdate($month, $day, $year)) {
$dt = "$year-$month-$day" ;
return strftime("%d-%b-%Y", strtotime($dt));
//return TRUE;
} else {
return "not a date" ;
}
}
?>
I have used the following code to validate dates coming from ExtJS applications.
function check_sql_date_format($date) {
$date = substr($date, 0, 10);
list($year, $month, $day) = explode('-', $date);
if (!is_numeric($year) || !is_numeric($month) || !is_numeric($day)) {
return false;
}
return checkdate($month, $day, $year);
}
<?php
function is_date( $str ) {
$stamp = strtotime( $str );
if (!is_numeric($stamp)) {
return FALSE;
}
$month = date( 'm', $stamp );
$day = date( 'd', $stamp );
$year = date( 'Y', $stamp );
if (checkdate($month, $day, $year)) {
return TRUE;
}
return FALSE;
}
?>
Related
I want to convert any Gregorian date to an equivalent Ethiopian Date. The Gregorian date is generated on records created or updated using the PHP date function, date('Y-m-d H:i:s') from the server date. I want to show the changes made are in the Ethiopian date so the views are can easily understand it. I've made a function to change it. but the function date is not reliable in some leap years.
The function returns the date format in either Tigrigna or Amharic language.
Any hint?
function conver_to_ethiopian_date($date, $lang ="ti"){
$OFFSET=79372;
$DAY=86400;
$EYear=1745;
$EMonth=0;
$EDate=0;
$GC=0;
$timezone = 0;
$tot= gmdate($date, 3600*($timezone+date("I")));
$days=round(strtotime($tot)/$DAY);
$days +=$OFFSET;
$mark=0;
$EYear=1745;
while ($mark==0){
if ($EYear % 4 ==3){
if ($days>=366) {
$days-=366;
$EYear=$EYear+1;
}
else{
$mark=1;
}
}
else{
if ($days>=365){
$days-=365;
$EYear=$EYear+1;
}
else {$mark=1;}
}
}
if ($days==0){
$EYear-=1;
//leap year
$EMonth=12;
$EDate=5 + (($EYear%4==3)?1:0);
}
else{
$EMonth = (($days-1) / 30);
if ($days % 30 ==0){ $EDate = 30;}
else {$EDate=$days % 30;}
}
$months_am =array("መስከረም", "ጥቅምት", "ኅዳር", "ታኅሣሥ", "ጥር","የካቲት","መጋቢት","ሚያዝያ", "ግንቦት", "ሰኔ", "ሐምሌ", "ነሐሴ", 'ጳጉሜ');
$months_tg =array("መስከረም", "ጥቅምቲ", "ሕዳር", "ታሕሳስ", "ጥሪ","የካቲት","መጋቢት","ሚያዝያ", "ጉንበት", "ሰነ", "ሓምለ", "ነሓሰ", 'ጳጉሜን');
$days_am =array("እሁድ", "ሰኞ", "ማክሰኞ", "ሮብ", "ሓሙስ", "ኣርብ", "ቀዳሜ");
$days_tg =array("ሰንበት", "ሶኒ", "ሰሉስ", "ረቡዕ", "ሓሙስ", "ዓርቢ", "ቀዳም");
$date_time = explode(' - ', $date);
$year =$year?$year:date("Y") ;
$day_of_week = date("w", mktime(0, 0, 0, $date_time[1], $date_time[2], $date_time[0]));
if ("am" == $lang)
return $days_am[$day_of_week]." ".$months_am[$EMonth]." ".$EDate.", ".$EYear;
else
return $days_tg[$day_of_week]." ".$months_tg[$EMonth]." ". $EDate.", ".$EYear;
}
I have a date in the form MM-YYYY (e.g: 04-2000). I exploded the date into month and year and am now trying to check certain conditions on the month:
between 1 and 12
formed of 2 digits)
and on the year:
formed of 4 digits
Is my syntax correct?
list($name_month, $name_year) = explode('-', $name_date, 2);
if(($name_month < 1 || $name_month > 12 || $name_month ) || ($name_year)) {
echo "<br><br>Wrong date";
$uploadOk = 0;}
You can use the DateTime object and createFromFormat() to validate your date:
$date = '04-2000';
// Create a DateTime object pointing to the 1st of your given month and year
$d = DateTime::createFromFormat('d-m-Y', '01-' . $date);
if($d && $d->format('m-Y') == $date){
echo 'Valid date';
}
Eval.in
This should work, if seprator is always -
$date = '04-2000';
if($date == date('m-Y', strtotime('01-'.$date)))
{
echo 'Valid date';
}
As commented, there are several was to do this. What first comes to mind is checkdate() and createFromFormat().
The catch is, you need to be mindful what is injected for the day part since your format does not include day.
Since createFromFormat() injects the day part from the current date, it is not a viable option. George's code will fail for a format of 02-2015 on days after 28.
As such, I would use checkdate():
$date = '04-2000';
list($month, $year) = explode('-', $date);
if (checkdate($month, 1, $year)) {
echo 'Valid date';
}
I know about the unwanted behaviour of PHP's function
strtotime
For example, when adding a month (+1 month) to dates like: 31.01.2011 -> 03.03.2011
I know it's not officially a PHP bug, and that this solution has some arguments behind it, but at least for me, this behavior has caused a lot waste of time (in the past and present) and I personally hate it.
What I found even stranger is that for example in:
MySQL: DATE_ADD('2011-01-31', INTERVAL 1 MONTH) returns 2011-02-28
or
C# where new DateTime(2011, 01, 31).AddMonths(1); will return 28.02.2011
wolframalpha.com giving 31.01.2013 + 1 month as input; will return Thursday, February 28, 2013
It sees to me that others have found a more decent solution to the stupid question that I saw alot in PHP bug reports "what day will it be, if I say we meet in a month from now" or something like that. The answer is: if 31 does not exists in next month, get me the last day of that month, but please stick to next month.
So MY QUESTION IS: is there a PHP function (written by somebody) that resolves this not officially recognized bug? As I don't think I am the only one who wants another behavior when adding / subtracting months.
I am particulary interested in solutions what also work not just for the end of the month, but a complete replacement of strtotime. Also the case strotime +n months should be also dealt with.
Happy coding!
what you need is to tell PHP to be smarter
$the_date = strtotime('31.01.2011');
echo date('r', strtotime('last day of next month', $the_date));
$the_date = strtotime('31.03.2011');
echo date('r', strtotime('last day of next month', $the_date));
assuming you are only interesting on the last day of next month
reference - http://www.php.net/manual/en/datetime.formats.relative.php
PHP devs surely don't consider this as bug. But in strtotime's docs there are few comments with solutions for your problem (look for 28th Feb examples ;)), i.e. this one extending DateTime class:
<?php
// this will give us 2010-02-28 ()
echo PHPDateTime::DateNextMonth(strftime('%F', strtotime("2010-01-31 00:00:00")), 31);
?>
Class PHPDateTime:
<?php
/**
* IA FrameWork
* #package: Classes & Object Oriented Programming
* #subpackage: Date & Time Manipulation
* #author: ItsAsh <ash at itsash dot co dot uk>
*/
final class PHPDateTime extends DateTime {
// Public Methods
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Calculate time difference between two dates
* ...
*/
public static function TimeDifference($date1, $date2)
$date1 = is_int($date1) ? $date1 : strtotime($date1);
$date2 = is_int($date2) ? $date2 : strtotime($date2);
if (($date1 !== false) && ($date2 !== false)) {
if ($date2 >= $date1) {
$diff = ($date2 - $date1);
if ($days = intval((floor($diff / 86400))))
$diff %= 86400;
if ($hours = intval((floor($diff / 3600))))
$diff %= 3600;
if ($minutes = intval((floor($diff / 60))))
$diff %= 60;
return array($days, $hours, $minutes, intval($diff));
}
}
return false;
}
/**
* Formatted time difference between two dates
*
* ...
*/
public static function StringTimeDifference($date1, $date2) {
$i = array();
list($d, $h, $m, $s) = (array) self::TimeDifference($date1, $date2);
if ($d > 0)
$i[] = sprintf('%d Days', $d);
if ($h > 0)
$i[] = sprintf('%d Hours', $h);
if (($d == 0) && ($m > 0))
$i[] = sprintf('%d Minutes', $m);
if (($h == 0) && ($s > 0))
$i[] = sprintf('%d Seconds', $s);
return count($i) ? implode(' ', $i) : 'Just Now';
}
/**
* Calculate the date next month
*
* ...
*/
public static function DateNextMonth($now, $date = 0) {
$mdate = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
list($y, $m, $d) = explode('-', (is_int($now) ? strftime('%F', $now) : $now));
if ($date)
$d = $date;
if (++$m == 2)
$d = (($y % 4) === 0) ? (($d <= 29) ? $d : 29) : (($d <= 28) ? $d : 28);
else
$d = ($d <= $mdate[$m]) ? $d : $mdate[$m];
return strftime('%F', mktime(0, 0, 0, $m, $d, $y));
}
}
?>
Here's the algorithm you can use. It should be simple enough to implement yourself.
Have the original date and the +1 month date in variables
Extract the month part of both variables
If the difference is greater than 1 month (or if the original is December and the other is not January) change the latter variable to the last day of the next month. You can use for example t in date() to get the last day: date( 't.m.Y' )
Had the same issue recently and ended up writing a class that handles adding/subtracting various time intervals to DateTime objects.
Here's the code:
https://gist.github.com/pavlepredic/6220041#file-gistfile1-php
I've been using this class for a while and it seems to work fine, but I'm really interested in some peer review. What you do is create a TimeInterval object (in your case, you would specify 1 month as the interval) and then call addToDate() method, making sure you set $preventMonthOverflow argument to true. The code will make sure that the resulting date does not overflow into next month.
Sample usage:
$int = new TimeInterval(1, TimeInterval::MONTH);
$date = date_create('2013-01-31');
$future = $int->addToDate($date, true);
echo $future->format('Y-m-d');
Resulting date is:
2013-02-28
Here is an implementation of an improved version of Juhana's answer above:
<?php
function sameDateNextMonth(DateTime $createdDate, DateTime $currentDate) {
$addMon = clone $currentDate;
$addMon->add(new DateInterval("P1M"));
$nextMon = clone $currentDate;
$nextMon->modify("last day of next month");
if ($addMon->format("n") == $nextMon->format("n")) {
$recurDay = $createdDate->format("j");
$daysInMon = $addMon->format("t");
$currentDay = $currentDate->format("j");
if ($recurDay > $currentDay && $recurDay <= $daysInMon) {
$addMon->setDate($addMon->format("Y"), $addMon->format("n"), $recurDay);
}
return $addMon;
} else {
return $nextMon;
}
}
This version takes $createdDate under the presumption that you are dealing with a recurring monthly period, such as a subscription, that started on a specific date, such as the 31st. It always takes $createdDate so late "recurs on" dates won't shift to lower values as they are pushed forward thru lesser-valued months (e.g., so all 29th, 30th or 31st recur dates won't eventually get stuck on the 28th after passing thru a non-leap-year February).
Here is some driver code to test the algorithm:
$createdDate = new DateTime("2015-03-31");
echo "created date = " . $createdDate->format("Y-m-d") . PHP_EOL;
$next = sameDateNextMonth($createdDate, $createdDate);
echo " next date = " . $next->format("Y-m-d") . PHP_EOL;
foreach(range(1, 12) as $i) {
$next = sameDateNextMonth($createdDate, $next);
echo " next date = " . $next->format("Y-m-d") . PHP_EOL;
}
Which outputs:
created date = 2015-03-31
next date = 2015-04-30
next date = 2015-05-31
next date = 2015-06-30
next date = 2015-07-31
next date = 2015-08-31
next date = 2015-09-30
next date = 2015-10-31
next date = 2015-11-30
next date = 2015-12-31
next date = 2016-01-31
next date = 2016-02-29
next date = 2016-03-31
next date = 2016-04-30
I have solved it by this way:
$startDate = date("Y-m-d");
$month = date("m",strtotime($startDate));
$nextmonth = date("m",strtotime("$startDate +1 month"));
if((($nextmonth-$month) > 1) || ($month == 12 && $nextmonth != 1))
{
$nextDate = date( 't.m.Y',strtotime("$initialDate +1 week"));
}else
{
$nextDate = date("Y-m-d",strtotime("$initialDate +1 month"));
}
echo $nextDate;
Somewhat similar to the Juhana's answer but more intuitive and less complications expected. Idea is like this:
Store original date and the +n month(s) date in variables
Extract the day part of both variables
If days do not match, subtract number of days from the future date
Plus side of this solution is that works for any date (not just the border dates) and it also works for subtracting months (by putting - instead of +).
Here is an example implementation:
$start = mktime(0,0,0,1,31,2015);
for ($contract = 0; $contract < 12; $contract++) {
$end = strtotime('+ ' . $contract . ' months', $start);
if (date('d', $start) != date('d', $end)) {
$end = strtotime('- ' . date('d', $end) . ' days', $end);
}
echo date('d-m-Y', $end) . '|';
}
And the output is following:
31-01-2015|28-02-2015|31-03-2015|30-04-2015|31-05-2015|30-06-2015|31-07-2015|31-08-2015|30-09-2015|31-10-2015|30-11-2015|31-12-2015|
function ldom($m,$y){
//return tha last date of a given month based on the month and the year
//(factors in leap years)
$first_day= strtotime (date($m.'/1/'.$y));
$next_month = date('m',strtotime ( '+32 day' , $first_day)) ;
$last_day= strtotime ( '-1 day' , strtotime (date($next_month.'/1/'.$y)) ) ;
return $last_day;
}
User enters the date as 8/1/11 or 08/1/11. Regardless of how the user inputs the date in my text field, I want to convert what they enter to my format of mm/dd/yyyy
So if the user enters 8/1/11 it would convert it to 08/01/2011.
Use strtotime() to transfer it into a time value, and then mktime() to transfer it to whatever format you want.
EDIT: Turns out it's not that simple.
You can't just print any date you'd like and transfer it to another, it'll be best to use some sort of UI on the client side to ensure the input matches, a great example is jQueryUI Datepicker
Check this.
It also checks if the date is valid (with checkdate) and converts years from short to long. When using short years, 0-69 is converted to 2000-2069 and 70-99 is converted to 1970-1999.
<?php
function cleanDate($input)
{
$parts = explode('/', $input);
if(count($parts) != 3) return false;
$month = (int)$parts[0];
$day = (int)$parts[1];
$year = (int)$parts[2];
if($year < 100)
{
if($year < 70)
{
$year += 2000;
}
else
{
$year += 1900;
}
}
if(!checkdate($month, $day, $year)) return false;
return sprintf('%02d/%02d/%d', $month, $day, $year);
// OR
$time = mktime(0, 0, 0, $month, $day, $year);
return date('m/d/Y', $time);
}
$test = array(
'08/01/2011', '8/1/11', '08/01/11', // Should all return 08/01/2011
'08/1/87', // Should return 08/01/1987
'32/1/93', '13', // Should fail: invalid dates
'02/29/2011', // Should fail: 2011 is not a leap year
'2/29/08'); // Should return 02/29/2008 (2008 is a leap year)
foreach($test as $t)
{
echo $t.' : '.(cleanDate($t) ?: 'false')."\n";
}
?>
Result:
08/01/2011 : 08/01/2011
8/1/11 : 08/01/2011
08/01/11 : 08/01/2011
08/1/87 : 08/01/1987
32/1/93 : false
13 : false
02/29/2011 : false
2/29/08 : 02/29/2008
<?php
$userInput = '08/1/11'; // or = '8/1/11' or = '08/01/11' or = '01/8/11'
$arr = explode('/', $userInput);
$formatted = sprintf("%1$02d", $arr[0]) . '/' . sprintf("%1$02d", $arr[1]) . '/20' . $arr[2];
?>
<?php
preg_match('#^(0?[1-9]|1[0-2])/(0?[1-9]|[12][0-9]|3[01])/(\d{2})$#',trim($date),$matches);
$month=str_pad($matches[1],2,'0',STR_PAD_LEFT);
$day=str_pad($matches[2],2,'0',STR_PAD_LEFT);
$year=$matches[3];
$result="{$month}/{$day}/{$year}";
?>
While #Truth is right that the user can do a lot to make it difficult, there IS actually a way to do a fairly decent job of parsing a date input box to work. It takes into account a variety of user input issues that may come up.
Note that it does make two assumptions:
That the local uses a date format of m/d/y
If the year is entered in short form, we are dealing with a 2000+ year
<?php
// Trim spaces from beginning/end
$date = trim(request($field));
// Allow for the user to have separated by spaces
$date = str_replace(" ", "/", $date);
// Allow for the user to have separated by dashes or periods
$date = str_replace("-", "/", str_replace(".", "/", trim($date)));
// Explode the date parts out to ensure a year
// Granted, this is geo-centric - you could adjust based on your locale
$dateparts = explode("/", $date);
// Check for a year. If not entered, assume this year
if (!isset($dateparts[2])) {$dateparts[2] = date("Y");}
// Force year to integer for comparison
$dateparts[2] = (int)$dateparts[2];
// Allow for user to use short year. Assumes all dates will be in the year 2000+
if ($dateparts[2] < 2000) {$dateparts[2]+= 2000;}
// Re-assemble the date to a string
$date = implode("/", $dateparts);
// Utilize strtotime and date to convert date to standard format
$date = date("m/d/Y", strtotime($date));
?>
Disclaimer: Yes, this is the verbose way of doing this, however I did so for clarity of the example, not for efficiency.
I have been developing on php 5.3.
However our production server is 5.2.6.
I have been using
$schedule = '31/03/2011 01:22 pm'; // example input
if (empty($schedule))
$schedule = date('Y-m-d H:i:s');
else {
$schedule = dateTime::createFromFormat('d/m/Y h:i a', $schedule);
$schedule = $schedule->format('Y-m-d H:i:s');
}
echo $schedule;
However that function is not available in 5.2
What is the easiest way to get around this (no chance of a php upgrade).
just include the next code
function DEFINE_date_create_from_format()
{
function date_create_from_format( $dformat, $dvalue )
{
$schedule = $dvalue;
$schedule_format = str_replace(array('Y','m','d', 'H', 'i','a'),array('%Y','%m','%d', '%I', '%M', '%p' ) ,$dformat);
// %Y, %m and %d correspond to date()'s Y m and d.
// %I corresponds to H, %M to i and %p to a
$ugly = strptime($schedule, $schedule_format);
$ymd = sprintf(
// This is a format string that takes six total decimal
// arguments, then left-pads them with zeros to either
// 4 or 2 characters, as needed
'%04d-%02d-%02d %02d:%02d:%02d',
$ugly['tm_year'] + 1900, // This will be "111", so we need to add 1900.
$ugly['tm_mon'] + 1, // This will be the month minus one, so we add one.
$ugly['tm_mday'],
$ugly['tm_hour'],
$ugly['tm_min'],
$ugly['tm_sec']
);
$new_schedule = new DateTime($ymd);
return $new_schedule;
}
}
if( !function_exists("date_create_from_format") )
DEFINE_date_create_from_format();
Because strtotime does poorly when confronted with D/M/Y and date_create_from_format isn't available, strptime may be your only hope here. It does some pretty oldschool things, like deal with years as if they are the number of years since 1900 and deal with months as if January was month zero. Here's some horrible example code that uses sprintf to reassemble the date into something DateTime understands:
$schedule = '31/03/2011 01:22 pm';
// %Y, %m and %d correspond to date()'s Y m and d.
// %I corresponds to H, %M to i and %p to a
$ugly = strptime($schedule, '%d/%m/%Y %I:%M %p');
$ymd = sprintf(
// This is a format string that takes six total decimal
// arguments, then left-pads them with zeros to either
// 4 or 2 characters, as needed
'%04d-%02d-%02d %02d:%02d:%02d',
$ugly['tm_year'] + 1900, // This will be "111", so we need to add 1900.
$ugly['tm_mon'] + 1, // This will be the month minus one, so we add one.
$ugly['tm_mday'],
$ugly['tm_hour'],
$ugly['tm_min'],
$ugly['tm_sec']
);
echo $ymd;
$new_schedule = new DateTime($ymd);
echo $new_schedule->format('Y-m-d H:i:s');
If it works, you should see the same, correct date and time printed twice.
I think it is much cleaner to extend the DateTime class and implement createFromFormat() yourself like this:-
class MyDateTime extends DateTime
{
public static function createFromFormat($format, $time, $timezone = null)
{
if(!$timezone) $timezone = new DateTimeZone(date_default_timezone_get());
$version = explode('.', phpversion());
if(((int)$version[0] >= 5 && (int)$version[1] >= 2 && (int)$version[2] > 17)){
return parent::createFromFormat($format, $time, $timezone);
}
return new DateTime(date($format, strtotime($time)), $timezone);
}
}
$dateTime = MyDateTime::createFromFormat('Y-m-d', '2013-6-13');
var_dump($dateTime);
var_dump($dateTime->format('Y-m-d'));
This will work in all versions of PHP >= 5.2.0.
See here for a demo http://3v4l.org/djucq
Since this is not really showing how to convert YYYY:DDD:HH:MM:SS time to unix seconds using the "z" option you have to create you own functions to convert the DOY to month and day of month. This is what I did:
function _IsLeapYear ($Year)
{
$LeapYear = 0;
# Leap years are divisible by 4, but not by 100, unless by 400
if ( ( $Year % 4 == 0 ) || ( $Year % 100 == 0 ) || ( $Year % 400 == 0 ) ) {
$LeapYear = 1;
}
return $LeapYear;
}
function _DaysInMonth ($Year, $Month)
{
$DaysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
return ((_IsLeapYear($Year) && $Month == 2) ? 29 : $DaysInMonth[$Month - 1]);
}
function yydddhhssmmToTime($Year, $DOY, $Hour, $Min, $Sec)
{
$Day = $DOY;
for ($Month = 1; $Day > _DaysInMonth($Year, $Month); $Month++) {
$Day -= _DaysInMonth($Year, $Month);
}
$DayOfMonth = $Day;
return mktime($Hour, $Min, $Sec, $Month, $DayOfMonth, $Year);
}
$timeSec = yydddhhssmmToTime(2016, 365, 23, 23, 23);
$str = date("m/d/Y H:i:s", $timeSec);
echo "unix seconds: " . $timeis . " " . $str ."<br>";
The output on the page shows its working since I can convert back the seconds back to the original input values.
unix seconds: 1483140203 12/30/2016 23:23:23
$your_datetime_object=new DateTime($date);
$date_format_modified=date_format($your_datetime_object,'D M d Y');//Change the format of date time
I had the similar problem with the production server at 5.2, so I used the above datetime to create an object and then change the format to my liking as above.
Date and Time only
$dateTime = DateTime::createFromFormat('Y-m-d\TH:i:s', '2015-04-20T18:56:42');
ISO8601 no colons
$dateTime = DateTime::createFromFormat('Y-m-d\TH:i:sO', '2015-04-20T18:56:42+0000');
ISO8601 with colons
$date = $dateTime->format('c');
Salesforce ISO8601 format
DateTime::createFromFormat('Y-m-d\TH:i:s.uO', '2015-04-20T18:56:42.000+0000');
Hope this saves someone time!