Parse string to timestamp [duplicate] - php

I have some code (it's part of a wordpress plugin) which takes a text string, and the format specifier given to date(), and attempts to parse it into an array containing hour, minute, second, day, month, year.
Currently, I use the following code (note that strtotime is horribly unreliable with things like 01/02/03)
// $format contains the string originally given to date(), and $content is the rendered string
if (function_exists('date_parse_from_format')) {
$content_parsed = date_parse_from_format($format, $content);
} else {
$content = preg_replace("([0-9]st|nd|rd|th)","\\1",$content);
$content_parsed = strptime($content, dateFormatToStrftime($format));
$content_parsed['hour']=$content_parsed['tm_hour'];
$content_parsed['minute']=$content_parsed['tm_min'];
$content_parsed['day']=$content_parsed['tm_mday'];
$content_parsed['month']=$content_parsed['tm_mon'] + 1;
$content_parsed['year']=$content_parsed['tm_year'] + 1900;
}
This actually works fairly well, and seems to handle every combination I've thrown at it.
However, recently someone gave me 24 Ноябрь, 2010. This is Russian for November 24, 2010 [the date format was j F, Y], and it is parsed as year = 2010, month = null, day = 24.
Are there any functions that I can use that know how to translate both November and Ноябрь into 11?
EDIT:
Running print_r(setlocale(LC_ALL, 0)); returns C. Switching back to strptime() seems to fix the problem, but the docs warn:
Internally, this function calls the strptime() function provided by the system's C library. This function can exhibit noticeably different behaviour across different operating systems. The use of date_parse_from_format(), which does not suffer from these issues, is recommended on PHP 5.3.0 and later.
Is date_parse_from_format() the correct API, and if so, how do I get it to recognize the language?

Try to set the locale to Russian as hinted in the manual:
Month and weekday names and other language dependent strings respect the current locale set with setlocale() (LC_TIME).

you could try take a locale parameter and call locale_set_default($locale) before doing the date parsing.
$originalLocale = locale_get_default();
$locale ? $locale : $originalLocale;
locale_set_default(locale);
// date parsing code
locale_set_default($originalLocale);
I haven't testing this but it's work a try.
FYI, I believe the locale string for Russian is "ru-Latn"

I see that the question is already answered but none of the solutions provided worked for me.
This is my solution:
if(!preg_match('/^en_US/', $locale)){
$months_short = array('jan' => t('jan'), 'feb' => t('feb'), 'mar' => t('mar'), 'apr' => t('apr'),
'may' => t('may'), 'jun' => t('giu'), 'jul' => t('lug'), 'aug' => t('ago'),
'sep' => t('set'), 'oct' => t('ott'), 'nov' => t('nov'), 'dec' => t('dec'));
foreach ($months_short as $month_short => $month_short_translated) {
$date = preg_replace('/'.$month_short_translated.'/', $month_short, strtolower($date));
}
}
$pieces = date_parse_from_format($format,$date);
if($pieces && $pieces['error_count'] == 0 && checkdate($pieces['month'], $pieces['day'], $pieces['year'])){
return date('Y-m-d', mktime(0,0,0,$pieces['month'],$pieces['day'],$pieces['year']));
}
Where t() returns the translated abbreviation for the month.
Probably not the best solution ever (because it fails if there is no valid translation) but it works for my case.

Related

How to get timestamp like this : 0000-00-00 00:00:00.000000 in php [duplicate]

I echo this :
php> echo date("Y-m-d\TH:i:s");
2011-05-27T11:21:23
How can do with date function to get this date format:
2011-01-12T14:41:35.7042252+01:00 (for example)
35.7042252 => seconds.decimal-fraction-of-second
I have tried:
php> function getTimestamp()
... {
... return date("Y-m-d\TH:i:s") . substr((string)microtime(), 1, 8);
... }
php> echo getTimestamp();
2011-05-27T15:34:35.6688370 // missing +01:00 how can I do?
date('Y-m-d\TH:i:s.uP')
u for microseconds was added in PHP 5.2.2. For earlier or (still) broken versions (see comments):
date('Y-m-d\TH:i:s') . substr(microtime(), 1, 8) . date('P')
Or, to avoid two calls to date:
date(sprintf('Y-m-d\TH:i:s%sP', substr(microtime(), 1, 8)))
Best performance:
substr_replace(date('c'), substr(microtime(), 1, 8), 19, 0);
By doing separate [date] calls you have a small chance of two time stamps being out of order: eg call date at 1:29:22.999999 and mircotime at 1:29:23.000001. On my server consecutive calls are about 10 us apart.
Source
Try this instead:
list($usec, $sec) = explode(" ", microtime());
echo date("Y-m-d\TH:i:s", $sec) . substr($usec, 1, 8) . date("P", $sec);
E.g.:
2015-07-19T16:59:16.0113674-07:00
As a solution for 2020 with the DateTime class, which was added in PHP 5.2, you can do a simple one liner to get the wanted format.
For example:
echo (new DateTime())->format('Y-m-d\TH:i:s.uP');
// 2020-04-23T09:18:25.311075+02:00
The DateTimeInterface, which is implemented by the DateTime class, brings its own constants for widely used date formats. If you know the format, you can use a constant for that.
echo var_dump($datetime->format(DateTime::RFC3339_EXTENDED));
// 2020-04-23T09:18:25.311+02:00
As object orientated programming is widely spread in the PHP world, this could be a possible solution, too.
If parsing the string returned by microtime makes you vomit in your mouth, and you don't want multiple distinct timestamps munged together into your output, you can do this:
$unow = microtime(true);
sprintf("%s.%06d%s", date("Y-m-d\TH:i:s", $unow), ($unow - floor($unow))*1e6, date("P", $unow));

Sort array of objects by closest date

I have an array of objects in PHP with one key - date. I have future dates as well as past dates. I need to sort them in by most current date.
Here is my array from var_dump()
array (size=15)
0 =>
object(stdClass)[7852]
public 'date' => string '20200417' (length=8)
1 =>
object(stdClass)[7846]
public 'date' => string '20200302' (length=8)
2 =>
object(stdClass)[7856]
public 'date' => string '20200224' (length=8)
3 =>
object(stdClass)[7853]
public 'date' => string '20200220' (length=8)
4 =>
object(stdClass)[7847]
public 'date' => string '20200213' (length=8)
5 =>
object(stdClass)[7845]
public 'date' => string '20200211' (length=8)
...
Is there a way to sort this so that the first object would be the 20200302, then 20200224 and so on. but the future dates would be at the very end of the array.
I've tried this:
usort($arr, function($a, $b) {
return strtotime($b->date) - strtotime($a->date);
});
But this code doesn't care about the current date, it just sorts them as dates.
I have also tried a workaround with calculating interval from the date('Ymd') - $arr->date and setting that as a part of an object, but strcmp() in usrot function gives virtually the same result, because future date will give negative number and in ascending order negative number will come before positive numbers. Meaning, the most future date will be first, not the most current date.
Any help is much appreciated.
What you need to do is work out how far each date is away from today and use this to sort the items, there may be a more elegant way, but this (I think) works. Basically use abs() to ensure all differences are +ve and take each date away from today...
usort($arr, function($a, $b) {
return (abs(strtotime('today') - strtotime($a->date))
- (abs(strtotime('today') - strtotime($b->date))));
});
Nigel's answer was a big help, but it produced uneven results, sometimes the dates would be all over the place, I have no idea why. So, I figured a workaround, perhaps it will be of help to someone on the internet.
I've added one more key/value to an object - interval, value for this key I calculated in the following manner.
$interval_raw = date("Ymd") - get_field('date_nachalo') + 1;
if($interval_raw < 0) {
$interval_raw += 10000;
}
So, if I get a negative number which is any future date, I'll add 10000 which is like 30 years in advance, a bit overkill I guess because no normal human schedules events that far into the future. Also, added + 1 so that the next upcoming date would be first. If I remove that, then the first date will be the date from the past. Basically, if today is march 10th and the last event was on march 1st and the next event event is on march 11th, without the + 1 the first event date would be the march 1st not the march 11th.
If anyone knows more elegant approach to this, do let the world know.

Problem to convert unix time to human time

I have a online controller where i want to know the uptime and last seen of my access points. for this i use the epoch convert methode to convert unix time to human readable time.
this is the code i use
// getting controller ap info //
$name=$status=$uptime=$last_seen=[];
foreach ($ligowave->result as $index => $obj) {
$name[] = $obj->name;
$status[] = $obj->status;
$uptime[] = $obj->uptime;
$last_seen[] = $obj->last_seen;
}
// time settings //
$epoch = $uptime;
$uptimetime = (new DateTime("#$epoch"))->format(' H:i:s');
$epoch = $last_seen;
$lastseendate = (new DateTime("#$epoch"))->SetTimeZone(new DateTimeZone('Europe/Amsterdam'))->format(' d-m-Y H:i:s');
if ($status == "up") {
echo $name;
echo " is up, ";
echo "uptime is:" . $uptimetime;
} else {
echo $name;
echo " is down, ";
echo "device is last seen:" . $lastseendate;
}
return array($name, $status, $epoch, $uptimetime, $lastseendate);
}
the error i am getting is:
PHP Fatal error: Uncaught Exception: DateTime::__construct(): Failed
to parse time string (#Array) at position 0 (#): Unexpected character
Please Note:
This answer is only about resolving the specific PHP error as stated on the question. For a wider answer about how to effectively write code to iterate over arrays please see Fyrye's answer.
The error you are receiving is:
PHP Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (#Array) at position 0 (#): Unexpected character
What is actually wrong, and why?
1)
You have an uncaught Exception, which throws a fatal error due to being, er, uncaught. Exceptions are central to object programming and should be researched and implemented in your PHP scripts.
2)
Why do you have an Exception in the first place? The error states the exception is caused by "Failed to parse time string (#Array)". So you are trying to give the DateTime object an array when it expects a string. It would help you to Read the PHP DateTime __construct Manual Page.
3)
Further, and more specifically the # character is unexpected; which means it should not be there. This character is only valid when followed by timestamp integer values in string format.
Because the array is output as a string (i.e in quotes) the end result is "#Array" and so the # is taken literally by DateTime; but of course this character is not expected by DateTime in any non-numeric incoming time string. This is the root cause of your fatal error here.
While PHP does employ loose typecasting to some extent, wrapping an array $var in quotes is far too loose and so the array simply outputs "Array" and issues a corresponding PHP Notice:
Notice: Array to string conversion in .....
For a valid list of correct DateTime string formats to give the object you can view this page from the PHP Manual.
4)
I do not see why you need those outer brackets?
So, how should this be done?
Reading the issues in reverse order from 4 to 1; the correct way of resolving this specific error is:
Wrap the attempt into a try/catch block to avoid these fatal errors.
Ensure that the value given to the DateTime object is a string
Remove unnecessary and invalid characters from that string.
So:
$epoch = $uptime;
try{
/***
* Uptime appears to be a numeric array of time string values
* Therefore go for the first one.
* You may want to wrap this code in a loop to catch each one.
***/
$uptimeTime = new DateTime("#".(string)$epoch[0]);
}
catch (Exception $ex){
/***
* If you wish to ignore these errors simply leave this block empty
***/
error_log("There was a problem on line ".__LINE__."! ".print_r($ex));
}
/***
* By default UTC timestamps are not zoned.
***/
// $uptimeTime->setTimeZone(new DateTimeZone('Europe/Amsterdam'));
$uptimeTimeOutput = $uptimeTime->format('H:i:s');
/***
* $uptimeTimeOutput is the correctly formatted date from $epoch[0]
***/
print $uptimeTimeOutput;
I hope with the information given above your able to correct the second DateTime instantiation code (ie the new DateTime line) yourself. :-)
TL;DR
Please read the PHP Manual and allow it to inform your coding choices.
The main issue is caused by defining $uptime[] as an array of values, resulting in $epoch = $uptime containing an array of timestamp strings. When DateTime() expects a single string value.
To resolve the issue you need to move the DateTime calls inside of the foreach iteration.
The other issue, as mentioned in the answer provided by Martin, is that you are not handling exceptions within your code. If uptime or last_seen is not of an expected value that is being supplied to the DateTime constructor, an exception will be thrown.
To handle the exceptions you can use atry/catch block in order to handle an issue that arises in your code. Exceptions are meant to point you to fatal errors in your code so that you can resolve or verify them programmatically and typically should not be ignored by using try/catch. For more details please see https://www.php.net/manual/en/language.exceptions.php
Without knowing exactly what you're trying to accomplish with your code. It appears you are wanting to echo and return all of the values from $ligowave->result. I made the appropriate changes below to reflect what I surmise are your intentions. Along with some minor simplifications.
Please clarify what you are wanting to return and echo and I will adjust my answer.
Example: https://3v4l.org/O0nS6
//...
// getting controller ap info //
$values = [];
foreach ($ligowave->result as $index => $obj) {
//convert the unix timestamps to DateTime objects
$uptime = (new DateTime('#' . $obj->uptime));
$last_seen = (new DateTime('#' . $obj->last_seen))->setTimeZone(new DateTimeZone('Europe/Amsterdam'));
//store the return values into an array
$values[] = $value = [
'name' => $obj->name,
'status' => $obj->status,
'uptimetime' => $uptime->format('H:i:s'),
'lastseendate' => $last_seen->format('d-m-Y H:i:s')
];
//output each of the statuses
printf('%s is %s, ', $obj->name, $obj->status);
if ('up' === $obj->status) {
echo 'uptime is: ' . $value['uptimetime'];
} else {
echo 'device is last seen: ' . $value['lastseendate'];
}
}
return $values;
}
Result:
foo is up, uptime is: 10:20:54
bar is down, device is last seen: 01-06-2019 08:22:30
Returns:
array (
0 =>
array (
'name' => 'foo',
'status' => 'up',
'uptimetime' => '10:20:54',
'lastseendate' => '05-06-2019 11:16:21',
),
1 =>
array (
'name' => 'bar',
'status' => 'down',
'uptimetime' => '10:20:54',
'lastseendate' => '01-06-2019 08:22:30',
),
)
It also appears that you are using a 24 hour time, to represent a duration.
If so you will need to use a DateInterval instead of DateTime, by using DateTime::diff from an appropriate timeframe. For more details please see https://php.net/manual/en/class.dateinterval.php
Assuming uptime is the started time and last_seen is the current run time, you can use $uptime->diff($last_seen), to retrieve the time that elapsed between uptime to last_seen (duration), instead of the 24 hour time value of uptime. Otherwise you can use $uptime->diff(new DateTime('now', new DateTimeZone('Europe/Amsterdam'))), to use the current date time.
One caveat, is that the hours of DateInterval are non cumulative, meaning you would need to add the days in some manner. I have used the most accurate of %a as opposed to adding on to the hours with days * 24
Example: https://3v4l.org/LHdqL
//...
$uptime = (new DateTime('#' . $obj->uptime))->setTimeZone(new DateTimeZone('Europe/Amsterdam'));
$last_seen = (new DateTime('#' . $obj->last_seen))->setTimeZone(new DateTimeZone('Europe/Amsterdam'));
$values[] = $value = [
'name' => $obj->name,
'status' => $obj->status,
'lastseendate' => $last_seen->format('d-m-Y H:i:s'),
'uptimetime' => $uptime->diff($last_seen)->format('%a.%H:%I:%S'), //DD.HH:MM:SS
];
//...
Result:
foo is up, uptime is: 12.22:48:26

preg_match for mysql date format

im trying to validate a date to see if it matchs the mysql format
this is the code
$match = "/^\d{4}-\d{2}-\d{2} [0-2][0-3]:[0-5][0-9]:[0-5][0-9]$/";
$s = $this->input->post("report_start"). " " . $this->input->post("report_start_time").":00";
$e = $this->input->post("report_end"). " " . $this->input->post("report_end_time").":59";
if($this->input->post("action") != "")
{
echo trim($s). " => " . preg_match($match, trim($s));
echo "<br>";
echo trim($e). " => " . preg_match($match, trim($e));
}
the date format goes into $s and $e are
$s = 2011-03-01 00:00:00
$e = 2011-03-01 23:59:59
and they both return false (0).
i tested the pattern on http://www.spaweditor.com/scripts/regex/index.php and it returns true (1)
http://pastebin.com/pFZSKYpj
however if i manual inter the date strings into preg_match like
preg_match($match, "2011-03-01 00:00:00")
it works.
i have no idea what im doing wrong
======================
now that i think about it, i only need to validate the houre:min part of the datetime string.
im manually adding the seconds and the date is forced by a datepicker and users cant edit it
You're making your work harder that it needs to be. In php there are many date handling functions that mean you don't have to treat dates like strings. So, rather than test that your input dates are in the correct format, just insist on the correct format:
$adate= date_create('January 6, 1983 1:30pm'); //date format that you don't want
$mysqldate= $adate->format("Y-m-d h:i:s");//date format that you do want
There are also functions to check that a date is a real date, like checkdate.
ok heres wat i did.
since im forcing the date format and the ending seconds of the time part
i just validated the hour:mini part using "/^2[0-3]|[01][0-9]:[0-5][0-9]$";
and if that returns true i put everything together end reconstructed the final datetime string
$match = "/^2[0-3]|[01][0-9]:[0-5][0-9]$/";
$s_d = $this->input->post("report_start");
$s_t = $this->input->post("report_start_time");
$e_d = $this->input->post("report_end");
$e_t = $this->input->post("report_end_time");
if($this->input->post("action") != "")
{
if(
( preg_match($match , trim($s_d." ".$s_t.":00")) )
&& ( preg_match($match , trim($e_d." ".$e_t.":59")) )
)
{
$r = $this->model_report->client_hours_logged(array($s,$e));
$data['report'] = $r;
var_dump($r);
//$this->load->view("report/client_hours_per_client",$data);
}
}
Watch out:
[0-2][0-3] is not a good regex for hour values - it will match 01, 12, 23 and others, but it will fail 04 through 09 and 14 through 19.
Better use (2[0-3]|[01][0-9]) instead.
I use this to validate a 'Y-m-d H:i:s' format date string:
match = '/^[12][0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[01]) ([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/';
You could use strtotime and date to parse and format the date properly.
Why not just simply force the date into the format you want:
$e = '2011-03-01 00:00:00';
$mysqlFormat = date('Y-m-d H:i:s', strtotime($e));
Also, there is a bit of an error in your regex [0-2][0-3]:[0-5][0-9]:[0-5][0-9] will only match the hours of 00,01,02,03,10,11,12,13,20,21,22,23 so it will never match 4am, or 3pm among others. That aside I looked over your RegEx and I don't see any problems with it matching the test cases you've offered. I would check to make sure there is not extra whitespace on either side of date string with trim().
I concur with Tim : MySQL behaves in quirks mode and always tries to go easy on DATE and DATE_TIME column types. You can omit certain parts of your input and it still will try to compensate and achieve that goal successfully to some degree... That's why, most numbers your Reg-ex considers as invalid, MySQL will accept as valid.

Parsing localized date strings in PHP

I have some code (it's part of a wordpress plugin) which takes a text string, and the format specifier given to date(), and attempts to parse it into an array containing hour, minute, second, day, month, year.
Currently, I use the following code (note that strtotime is horribly unreliable with things like 01/02/03)
// $format contains the string originally given to date(), and $content is the rendered string
if (function_exists('date_parse_from_format')) {
$content_parsed = date_parse_from_format($format, $content);
} else {
$content = preg_replace("([0-9]st|nd|rd|th)","\\1",$content);
$content_parsed = strptime($content, dateFormatToStrftime($format));
$content_parsed['hour']=$content_parsed['tm_hour'];
$content_parsed['minute']=$content_parsed['tm_min'];
$content_parsed['day']=$content_parsed['tm_mday'];
$content_parsed['month']=$content_parsed['tm_mon'] + 1;
$content_parsed['year']=$content_parsed['tm_year'] + 1900;
}
This actually works fairly well, and seems to handle every combination I've thrown at it.
However, recently someone gave me 24 Ноябрь, 2010. This is Russian for November 24, 2010 [the date format was j F, Y], and it is parsed as year = 2010, month = null, day = 24.
Are there any functions that I can use that know how to translate both November and Ноябрь into 11?
EDIT:
Running print_r(setlocale(LC_ALL, 0)); returns C. Switching back to strptime() seems to fix the problem, but the docs warn:
Internally, this function calls the strptime() function provided by the system's C library. This function can exhibit noticeably different behaviour across different operating systems. The use of date_parse_from_format(), which does not suffer from these issues, is recommended on PHP 5.3.0 and later.
Is date_parse_from_format() the correct API, and if so, how do I get it to recognize the language?
Try to set the locale to Russian as hinted in the manual:
Month and weekday names and other language dependent strings respect the current locale set with setlocale() (LC_TIME).
you could try take a locale parameter and call locale_set_default($locale) before doing the date parsing.
$originalLocale = locale_get_default();
$locale ? $locale : $originalLocale;
locale_set_default(locale);
// date parsing code
locale_set_default($originalLocale);
I haven't testing this but it's work a try.
FYI, I believe the locale string for Russian is "ru-Latn"
I see that the question is already answered but none of the solutions provided worked for me.
This is my solution:
if(!preg_match('/^en_US/', $locale)){
$months_short = array('jan' => t('jan'), 'feb' => t('feb'), 'mar' => t('mar'), 'apr' => t('apr'),
'may' => t('may'), 'jun' => t('giu'), 'jul' => t('lug'), 'aug' => t('ago'),
'sep' => t('set'), 'oct' => t('ott'), 'nov' => t('nov'), 'dec' => t('dec'));
foreach ($months_short as $month_short => $month_short_translated) {
$date = preg_replace('/'.$month_short_translated.'/', $month_short, strtolower($date));
}
}
$pieces = date_parse_from_format($format,$date);
if($pieces && $pieces['error_count'] == 0 && checkdate($pieces['month'], $pieces['day'], $pieces['year'])){
return date('Y-m-d', mktime(0,0,0,$pieces['month'],$pieces['day'],$pieces['year']));
}
Where t() returns the translated abbreviation for the month.
Probably not the best solution ever (because it fails if there is no valid translation) but it works for my case.

Categories