Reg Exp For Ignoring White Space In Date Format - php

I am trying to create reg_exp for matching the given date string. In that date string following format is possible
01.12.1990
01. 12. 1990
I created the reg_exp for the first format (i.e) without space between day, month, year. But its not working for the second format too. How can i make the reg_exp which should support both and get the date?
My reg_exp is below,
$dateString = "01.12.1990";
preg_match("/^(0[1-9]|[1-2][0-9]|3[0-1]).(0[1-9]|1[0-2]).[0-9]{4}$/",$dateString))

Use \s
Read More about Escape
$dateString = "01 . 12 . 1990";
preg_match_all("/^(0[1-9]|[1-2][0-9]|3[0-1])\s*.\s*(0[1-9]|1[0-2])\s*.\s*[0-9]{4}$/is",$dateString,$res);
print_r($res);

The following and using str_replace() will echo:
01.12.1990
Match
<?php
$dateString = "01. 12. 1990";
$dateString = str_replace(' ', '', $dateString);
echo $dateString;
if( preg_match("/^(0[1-9]|[1-2][0-9]|3[0-1]).(0[1-9]|1[0-2]).[0-9]{4}$/",$dateString)) {
echo "<br>";
echo "Match";
}
else{
echo "<br>";
echo "No match";
}
If using $dateString = "01. 12. 1990x"; with a letter inside will echo
01.12.1990x
No match

Why do not you work with dates by the strftime?
http://php.net/manual/en/function.strftime.php

Related

PHP - strtotime() return 1970

The following code snippet:
echo date("d.m.Y-H:i:s", strtotime("01.01.2000-11:12:32"));
returns me:
01.01.1970-01:00:00
What's the right way to convert "01.01.2000-11:12:32" to time/date object, for comparing it with the current timestamp?
e.g.
if (date("d.m.Y-H:i:s") > date("d.m.Y-H:i:s", strtotime("01.01.2000-11:12:32"))) {
echo "future";
} else {
echo "past";
}
This is due to localisation. Try giving a different format, as the format matters a lot:
echo date("d.m.Y-H:i:s", strtotime("01/01/2000 11:12:32"));
echo date("d.m.Y-H:i:s", strtotime("01-01-2000 11:12:32"));
You should not have . for date and month separator.
You cannot separate date and time using -.
If you are getting the input from another source, try using str_replace:
echo date("d.m.Y-H:i:s", strtotime(str_replace(array(".", "-"), array("/", " "), "01.01.2000-11:12:32")));
Output: http://ideone.com/d19ATK
Try to replace . with -:
echo date("d.m.Y-H:i:s", strtotime(str_replace('.', '-', "01.01.2000 11:12:32")));
Also remove - between the date and time.
You can use DateTime::createFromFormat
$date = DateTime::createFromFormat('d.m.Y-H:i:s', '01.01.2000-11:12:32');
$now = new DateTime();
if ($now > $date) {
echo "future";
} else {
echo "past";
}

changing ISO date-time format in php

I am getting ISO datetime using below code
$date= date("c");
echo $date;
which returns something like below
2016-01-07T20:18:46+00:00
But the API I use tells that its wrong format and it needs in below format:
2016-01-07T20:35:06+00Z
I need to remove :00 at the end and add Z.
I am completely new to regex , can anyone help me understand the regex and tell which format is required.
You'll want to define the date format specifically.
If microseconds will always be 00
date("Y-m-d H:i:s+00\Z");
Else, use this little big of logic
date("Y-m-d H:i:s+"). substr((string)microtime(), 2, 2) . 'Z';
More info.
http://php.net/manual/en/function.date.php
You can find the last occurrence of : with strrpos, and get the substring up to it with substr, and then add Z:
$date= date("c");
echo "Current: $date\n"; // => 2016-01-07T21:58:08+00:00
$new_date = substr($date, 0, strrpos($date, ":")) . "Z";
echo "New : " . $new_date; // => 2016-01-07T22:10:54+00Z
See demo

php date strtotime wrong unix tuimestamp

I am tryig to check timestamp using php. However, the $date variable echoes out a timestamp
1382389873 which I checked with a unix converter epochconverter.com it shows Mon, 21 Oct 2013 21:11:13 GMT when it is meant to be 5 Nov 2013. Anyone can see the code below and pinpount my mistake? Thanks.
$today = strtotime(date("d.m.y"));
$c_date = strtotime($CI->session->userdata('c_date'));
$early = strtotime(date("d.m.y")."+2 week");
$date = strtotime("5.11.13");
echo'<br/>';
echo 'test'.$date;
echo'<br/>';
echo 'collection'.$c_date;
echo'<br/>';
echo 'early'.$early;
echo'<br/>';
echo 'today '.$today;
echo'<br/>';
strtotime is very vague in the way it processes dates. It is interpreting "5.11.13" as 5:11:13pm today (Which is 21:11:13 on a 24-hour clock).
If you want to specify november 5th you should do it like so:
$date = strtotime("11/5/13");
echo $date;
echo date("m/d/Y", $date);
Output:
1383609600
11/05/2013
Others answered this before I did, but here are my tests and notes to supplement the conversation... strtotime() converts a string to a time integer. date() converts an integer to a (optionally formatted) string. I was gonna say you can always just use that rather than epochvonverter.com... but now I understand why you went there. Also note that the manual says this:
"Note: Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible." -http://www.php.net/manual/en/function.strtotime.php (more quirky things on that page, btw).
It is very interesting that this example with dot is NOT producing d.m.y, but h.m.s! I guess that's because we're working with strtoTIME, not... uh.. strtoDATE :)
Consider this:
$integer = 1382389873;
var_dump($integer);
echo "<br>";
//int(1382389873)
$string = date($integer);
var_dump($string);
echo "<br>";
//string(10) "1382389873"
$formatted=date('d.m.y',$integer);
$laterint=strtotime($formatted."+2 week");
var_dump($laterint);
echo "<br>";
// int(1383682213)
$laterstring=date('d.m.y',$laterint);
var_dump($laterstring);
echo "<br>";
// string(8) "05.11.13"
And here's the juicy part:
$date1 = strtotime("5.11.13");
var_dump($date1);
echo "<br>";
$datestring1=date('d.m.y',$date1);
var_dump($datestring1);
//BAD PHP, BAD!
echo "<br>";
$date2 = strtotime("5.11.2013");
var_dump($date2);
echo "<br>";
$datestring2=date('d.m.y',$date2);
var_dump($datestring2);
echo "<br>";
$date3 = strtotime("5/11/13");
var_dump($date3);
echo "<br>";
$datestring3=date('d.m.y',$date3);
var_dump($datestring3);
echo "<br>";
Really interesting stuff, guys - thank you all! The moral of the story for me is to always be explicit with 4 digit year.
I ran a couple of tests for you and it seems that you need to put your year in YYYY format e.g. 2013:
$date = strtotime("5.11.2013"); // your code
echo $date . "\n";
echo date('d-m-Y', $date);
// timestamp: 1383562800
// converted to date: 05-11-2013
--edit-- as I mentioned in my previous comment, if $date is supposed to be the same as $early, why are you even bothering to reassign it manually? Why not just use $early?

Convert date format in PHP (without SQL)

How to convert date from yyyy:mm:dd to yyyy-m-dd and yyyy:mm:dd (without leading zero for the month) to yyyy-m-dd (with leading zero to month)?
You could use DateTime::createFromFormat and then use DateTime::format.
Example:
$date = DateTime::createFromFormat('Y:m:d', '2012:08:02');
echo $date->format('Y-m-d');
// without leading zero for month
$date = DateTime::createFromFormat('Y:n:d', '2012:8:02');
echo $date->format('Y-m-d');
try this:
$dateFrom ="2012:8:2";
$dateTo = str_replace(":","-",$dateFrom);
$dateTo = date("Y-m-d", strtotime($dateTo));
echo $dateTo;
Use the $date=strtotime($date) function to get the date in unix timestamp. After that you can use the date("Y-m-d",$date) function to convert it to the format you want.Here's an example:
$date=strtotime($olddate);
$date=date("Y-m-d",$date);
echo $date; // Now this will show you the date in the format you wanted :)
Use the date function.
date will not understand : as a separator, so you will need to replace that with a separator that it understands, like / or -, with str_replace.
Code:
$orig_date = '2012:8:2';
$final_date = date('Y-n-d', str_replace(':', '/', $orig_date));
echo $final_date; // Result: 2012-8-02
Use date() function
echo date('Y-m-d'); // for 1st case (replacing ':' with '-')
echo date('Y-j-d'); // for 2nd case (without leading zero)

strtotime() converts a non existing date to another date

I am building a timestamp from the date, month and year values entered by users.
Suppose that the user inputs some wrong values and the date is "31-02-2012" which does not exist, then I have to get a false return. But here its converting it to another date nearby. Precisely to: "02-03-2012"..
I dont want this to happen..
$str = "31-02-2012";
echo date("d-m-Y",strtotime($str)); // Outputs 02-03-2012
Can anyone help? I dont want a timestamp to be returned if the date is not original.
You might look into checkdate.
That's because strtotime() has troubles with - since they are used to denote phrase like -1 week, etc...
Try
$str = '31-02-2012';
echo date('d-m-Y', strtotime(str_replace('-', '/', $str)));
However 31-02-2012 is not a valid English format, it should be 02-31-2012.
If you have PHP >= 5.3, you can use createFromFormat:
$str = '31-02-2012';
$d = DateTime::createFromFormat('d-m-Y', $str);
echo $d->format('d-m-Y');
You'll have to check if the date is possible before using strtotime. Strtotime will convert it to unix date meaning it will use seconds since... This means it will always be a date.
You can workaround this behavior
<?php
$str = "31-02-2012";
$unix = strtotime($str);
echo date('d-m-Y', $unix);
if (date('d-m-Y', $unix) != $str){
echo "wrong";
}
else{
echo date("d-m-Y", $unx);
}
or just use checkdate()
Use the checkdate function.
$str = "31-02-2012";
$years = explode("-", $str);
$valid_date = checkdate($years[1], $years[0], $years[2]);
Checkdate Function - PHP Manual & Explode Function - PHP Manual
Combine date_parse and checkdate to check if it's a valid time.
<?php
date_default_timezone_set('America/Chicago');
function is_valid_date($str) {
$date = date_parse($str);
return checkdate($date['month'], $date['day'], $date['year']);
}
print is_valid_date('31-02-2012') ? 'Yes' : 'No';
print "\n";
print is_valid_date('28-02-2012') ? 'Yes' : 'No';
print "\n";
Even though that date format is acceptable according to PHP date formats, it may still cause issues for date parsers because it's easy to confuse the month and day. For example, 02-03-2012, it's hard to tell if 02 is the month or the day. It's better to use the other more specific date parser examples here to first parse the date then check it with checkdate.

Categories