how to decompose time in different part in php? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to decompose a time in two part, I mean by that, I want to take the left side and the right side, like a cut off right ?
22:02:00
23:00:00
23:12:00
Imagine those number, it doesn't matter if there's seconds or not, so we can kick them
22:02
23:00
23:12
Now, I to take the separate hour and minute. How can we do that ?

The simplest way would be to just cut out the first 5 characters of the string:
$time = '22:02:00';
echo substr($time, 0, 5); // 22:02
You can also parse the time using e.g. the DateTime class:
$time = '22:02:00';
$parsed = DateTime::createFromFormat('H:i:s', $time);
echo $parsed->format('H:i');
As Mark Baker commented, the strptime() function can also be used:
$time = '22:02:00';
$parsed = strptime($time, '%H:%M');
echo str_pad($parsed['tm_hour'], 2, '0', STR_PAD_LEFT) . ':' . str_pad($parsed['tm_min'], 2, '0', STR_PAD_LEFT);
Regular expressions would also work:
$time = '22:02:00';
preg_match('/^(?P<hour>\d{2}):(?P<minute>\d{2})/', $time, $result);
if (count($result) > 0) {
echo "{$result['hour']}:{$result['minute']}";
}

Related

Format timezone offsets that starts with + or - [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How can I format a string to prepend UTC if it starts with + or - and add a : before the last two digits if it ends with 4 digits?
Examples and expected result:
PST > PST
+08 > UTC+08
-0845 > UTC-08:45
Thank you!
<?php
$string = "PST";
if (substr($string, 0, 1) === '+' || substr($string, 0, 1) === '-'){
if(strlen($string) == 3){
$newString = 'UTC'.$string;
}
else{
$newString = 'UTC'. substr($string, 0, 3). ':' .substr($string, -2, 2);
}
}
else{
$newString = $string;
}
echo $newString;
You don't need regex for that or at least regex seems overkill. You can simple create this logic using an if-else and modify your string accordingly

PHP - Filter an array if a row contains string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have the follow API output, i basicly want to grab the member ids when the field relative contains 'minutes' how can i do this with an array filter?
members:
239:
relative: "21 minutes ago"
941591:
relative: "5 hours ago"
4178:
relative: "59 minutes ago"
78:
relative: "2 hours ago"
(not sure on the correct terms) but "relative" is a header/field name below the member ids, and the field relative contains the values like: "59 minutes ago"
I know i can get my answer by looping through all members and checking the field, but i prefer not to do it this way
Answer that worked:
$new = array_filter($idcall_array['members'], function ($var) {
return strpos($var['relative'], 'minutes') !== false;
});
Extract the relative column and grep for ones that contain minutes:
$result = preg_grep('/minutes/', array_column($array['members'], 'relative'));
Or filter:
$result = array_filter($array['members'],
function($v) {
return strpos($v['relative'], 'minutes') !== false;
});
Then just get the keys:
$ids = array_keys($result);

Best approach to explode high values [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
What's best approach to explode and separate high numeric values so it will be displayed in more legible way?
For example
100000000 should be converted to 100 000 000, or 10000.00 to 10 000.00
Use the number_format function.
$number = 1234.56;
number_format($number, 2, ',', ' '); // 1 234,56
As it's tagged as PHP you are looking for function called
number_format()
More details how to use it in documentation http://php.net/manual/en/function.number-format.php
Try this:-
$value = 100000000;
echo number_format($value , 0, ' ', ' ');
Output:- 100 000 000
number_format() function will accept one, two, and four arguments. Not three. and it works as follows:
// formatting with ","
$number = 1234.56;
var_dump(number_format($number)); // 1,235
//formatting with decimals
$number = 1234.56;
var_dump(number_format($number, 2)); // 1,235.56
//formatting with thousands seperator
$number = 1234.56;
var_dump(number_format($number, 2, '.', '')); // 1235.56

PHP Match string with numbers and get next number (as Year) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
These are my strings
2012-13
2013-14
2014-15
2015-16
2016-17
2017-18
I my string is 2014-15 as current financial year, I want my next my next financial year as 2015-16
How can I achieve this with PHP? I get these year list form mysql table, but I want next financial year in php
Try this:
$financial_year = "2012-13";
$fin_array = explode("-", $financial_year);
$next_fin_array[0] = $fin_array[0] + 1;
$next_fin_array[1] = $fin_array[1] + 1;
$next_financial_year = implode("-", $next_fin_array); // Gives 2013-14
Hope this helps.
$myStr = '2014-15';
$yr1 = (int) substr($myStr, 0, 4);
$yr2 = (int) substr($myStr, -2);
echo ($yr1+1).'-'.($yr2+1);
// 2015-16
$financeYear = DateTime::createFromFormat ( "Y-d" ,"2014-15");
$financeYear->modify('+ 1 year');
echo $financeYear->format("Y-d");
Result
2015-15

How to change a value in PHP based on the day of the month [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
is there a simple way to change a variable for example $value='abcdef' to another value $value= 'ghijkl' in PHP based on the date/day of the month ?
So if it's for example the 15th or 20th of a month the value should change from "abcdef" to "ghijkl".
I would be glad if someone could help out.
You can use the date() function in php to extract a certain part of the current date. The 'j' selection option selects the day of the month (reference).
You can use a switch case or if statements to perform actions on this parameter, like so:
$value = 'abcdef';
if(date('j') == '15') {
$value = 'ghjkl';
}
Or by:
$value = 'abcdef';
switch(date('j')) {
case '20':
case '15':
$value = 'ghjkl';
break;
}
$value = date("j")=="5" ? "It's the fifth!" : "It's some other day";
date() is a function to print a date (by default the current date) and format it with format codes (letters). j is the format code for the day of the month. The ? is a tenary operator, it is a shorter equivalent to doing this:
if(date("j")=="5") {
$value = "It's the fifth!";
}
else {
$value = "It's some other day";
}
Hope that you now understand how to do that!

Categories