I have response from an API like below
2019-05-08T10:36:00+0530
I want to take 10:36 from it, right now I do something with regex like
preg_match('/T(.*?)\:00/');
It works, but is there any other actual way to do it more efficiently? That works on any time string in this format?
Create a DateTime object by passing that string into the constructor and just format it to your liking with format()! You might want H:i if you just want the hours and minutes, or H:i:s if you want to include the seconds.
$string = "2019-05-08T10:36:00+0530";
$date = new DateTime($string);
echo $date->format("H:i:s");
Live demo at https://3v4l.org/BmnoU
If your string format is fixed, you can use substr().
$string = "2019-05-08T10:36:00+0530";
echo substr($string, 11, 5);
Output:
10:36
Working Example:
Related
I'm not too good with dates in php (or any other language) and would need some help to convert a date's format into something more readable.
The string currently looks like this:
2021-03-31T00:00:00.0000000+02:00
But i would much rather prefer it to be similar to this when echo it:
2021-03-31 00:00:00
I have done some more or less useless stuff with string manipulation, but there must be a better way?
Example:
substr('2021-03-31T00:00:00.0000000+02:00',0,10);
How would one do this?
PHP has pretty extensive DateTime functionality that you can take advantage of, for this example you can use the DateTime class or DateTimeImmutable class.
$date = new DateTime('2021-03-31T00:00:00.0000000+02:00');
echo $date->format('Y-m-d H:i:s'); // 2021-03-31 00:00:00
You could also do it with string manipulation by
spliting string (e.g $datetime)with letter T to array by function explode
take first element and store it in a variable $date
take the second element of array and store it in $time (only 8 letters which represent time)
$datetime = '2021-03-31T00:00:00.0000000+02:00';
$date_time = explode('T', $datetime);
$date = $date_time[0];
$time = substr($date_time[1],0,8);
echo $date.' '.$time;//2021-03-31 00:00:00
I currently get duration in this format: 04:00 but I need in this format 00:04:00.
I am using this code but it's not working correctly.
$time = date("h:i:s", strtotime($duration));
echo $time;
try like this,
$duration = "00:"."04:00";
This should work for you:
(Your problem is in which format you read in your time, so DateTime::createFromFormat)
$date = DateTime::createFromFormat("i:s", "04:00");
echo $date->format('H:i:s');
Output:
00:04:00
The problem is the strtotime($duration); It doesn't know exactly what format that's in, and can't get a valid date from it. (This is why you're getting unexpected results from the date function.
If you want to just simply add 00: before $duration, you can do it like so:
$duration = '04:00';
$duration = '00:' . $duration;
You then would be able to pass this to the strtotime function, and would likely get better results.
I have a datetime string which I would like to crop the 'time' part out of.
I need to do some calculation on it so I need to convert it to Unix time stamp.
What I've tried:
Use substr, and then strtotime, but when checking the result back to a human readable time format, it is not the same as the original date.
function convert($dbTime){
$createDate = new DateTime($dbTime);
$strip = $createDate->format('Y-m-d');
$yearMonthDateArray = explode("-", $strip);
}
The explode here crashes.
Edit:
This is the value of dbTime: "2014-07-27 12:06:00"
I want it to be "2014-07-27", and then have strototime on this format. This does not work. Converting it back to human readable date it generates 2014-07-06
Regarding comments:
I have tried all sorts of datetime functions. They either crash or they don't return the proper time
Explode crashes - it doesn't continue to the next line of code.
Edit2:
This is what's going on next. This returns false
$timeWithMakeTime = mktime(0,0,0,(int)$yearMonthDateArray[0], (int)$yearMonthDateArray[1],(int)$yearMonthDateArray[2]);
Discarding all your messy code, I assume you just want this:
$dbTime = '2014-07-27 12:06:00';
$date = new DateTime($dbTime);
$date->setTime(0, 0, 0);
echo $date->getTimestamp();
DateTime is an object, so what if you want to get timestamp you can do is as follows:
function convert($dbTime){
$createDate = new DateTime($dbTime);
return $createDate->getTimestamp();
}
You can read more about getTimestamp and other DateTime functions
Make sure your variable $dbTime is in correct format. For example DateTime does not support split seconds.
$date = date('H:i:s', strtotime($dbTime));
echo $date;
In PHP, I'm saving a date record as string in order to make conditions like "more than", "less than"; but I would really like to know how to decode it. What I want to do is something like this:
The moment that record is being saved is 2014, 08/06 1:30 in 24-hours format, so my integer should be like 201408060130, of course for this I use date() function.
But when it comes to decoding it to show it back like 2014, 08/06 1:30 or another format like 08/06 2014, 1:30 I really get stuck thinking on any solution for this.
I thought it would be like:
$date = date('YdmHm'); //Saving as 201408060130
$this->saveToDatabase($save, $mytable);
$dateDecoded = $this->getFromDatabase($mytable, $id, $theDateInteger);
$result = Date::getFormat($dateDecoded, 'YdmHm'); //Decode date format
echo $result->date('Y d/m, H:m'); //Show 2014 08/06, 1:30
This is simple & works. Just use strtotime:
$test_value = '201408060130';
echo date('Y d/m, H:i', strtotime($test_value));
The output is:
2014 06/08, 01:30
PHP has a really useful method under the Date class for this called DateTime::createFromFormat()
Here is an example usage:
$datetime = DateTime::createFromFormat('YdmHm', (string)$theDateInteger);
Now this is a valid datetime object which you can save to any formatting in a string if desired. For example:
echo $datetime->date('Y d/m, H:m');
I use a radio button that gives me two variables concatenated and I need to separate them later. It gives me a time and a date in a format 11:30pm3, where 3 is the date and 11:30pm is the time. I can split it fine with my function but if the time is one digit, like 7:30pm for example, it throws things off. I can use military time but that's not what I want.Is there a way to change this so it splits the string right after character "m" so it will work for am/pm, regardless of the length of the time being 7:00am or 07:00am. Thanks in advance.
$string = $Radio; //This is the value I get
$MeetTime = substr("$string", 0, 7); //Gives me 11:30am
$MeetDay = substr("$string", 7, 2); //Gives me 2
find out where the m is and do your logic on that
if m is at 6 then its a full length one
$loc = stripos($string,"m");
if its at 5 then it's short so adjust your split
if(preg_match('~^([\\d]{1,2}:[\\d]{1,2}[ap]m)([\\d]+)$~i', trim($string), $Matches)){
var_dump($Matches); // See what this prints
}
Hope it helps.
try this:
$date = '11:30am3';
$date = explode('pm', $date);
if(count($date) <= 2){
$date = explode('am', $date[0]);
}
print_r($date);
where $date[1] is the time and $day[0] the date.
But you should use somet other format, I'd recommend timestamps.