I have a JSON of all bitcoin prices for previous 90 days. I'm trying to only work with the weekday prices, excluding weekend prices. This is how I am doing it. Could you please tell me what I'm doing wrong or point me in the right direction?
<?php
$string = file_get_contents("https://blockchain.info/charts/market-price?timespan=90days&format=json");
$btc_price = json_decode($string, true);
$allDays = $btc_price[values];
$weekends = array_filter($allDays[values][x], function($d) {
return (date('N', strtotime(date("Y-m-$d"))) >= 6);
});
$allDays[$year][$month] = array_diff($allDays[$year][$month], $weekends);
echo "<pre>";
print_r($allDays);
?>
You're fairly close. I've made a few changes to get it working:
Instead of passing in $allDays['values']['x'], you should pass in all of the days. This will allow you to remove each weekend day without having to do the array_diff step. $allDays['values']['x'] should be $allDays.
Your expression in the array_filter callable was wrong. I'm not clear what your intention was, but it looks like you were attempting to get the day of the week and filter on that. That's a good strategy, but your implementation wasn't effective. You can get the day of the week with w in date and pass in the unix timestamp you were given from the API. Also, checking for greater than 6 wasn't effective, since Sunday is 0.
$weekends = array_filter($allDays[values][x], function($d) {
return (date('N', strtotime(date("Y-m-$d"))) >= 6);
});
should be changed to:
$allDays = array_filter($allDays, function($d) {
return !(date('w', $d['x']) == 6 || date('w', $d['x']) == 0);
});
I've cleaned up some syntax. When accessing array keys, you should wrap your indexes in quotes. PHP was trying to interupt your keys as constants before falling back to the expected behavior. $allDays[values][x] should have been $allDays['values']['x'].
Here's the full snippet so you have context:
<?php
$string = file_get_contents("https://blockchain.info/charts/market-price?timespan=90days&format=json");
$btc_price = json_decode($string, true);
$allDays = $btc_price['values'];
$allDays = array_filter($allDays, function($d) {
return !(date('w', $d['x']) == 6 || date('w', $d['x']) == 0);
});
// This is just done to reset the keys in the array. It's entirely optional.
$allDays = array_values($allDays);
echo "<pre>";
print_r($allDays);
?>
Related
I have an array of digit time numbers (I get them from the input fields).
Here are a few examples:
00:00:10
00:03:00
01:20:00
My question is following, how do I check if a digit time is greater than another?
Following function works up to
00:01:00
After that I get an error.
$inputTimes = $request->input('times', []);
foreach ($inputTimes as $inputKey => $inputValue)
{
$Input = new Input();
$Input->input_start = $inputValue['input_start'];
$tInput = explode(':', $inputValue['input_timelapse']);
$implInput = implode('', $tInput);
$iImplInput = (int)$implInput;
// Check if time is greater
if($iImplInput > $iVideoDuration)
{
.. error time greater
}
}
I would convert it to Unix time by using the inbuild strtotime() function.
For example:
strtotime('01:20:00'); will output 1483492800 while strtotime('00:01:00'); will output 1483488060.
Hope this helped.
I have a range of dates in string format in the form of
'2014-10-12'
what i want to do is compare these dates so i can get the oldest and the youngest.
In PHP how do i convert these to a format where i can do the following?
$oldestdate;
$youngesdate;
//loop though all the dates
if($exampledate < $youngesdate)
$youesdate = $exampledate;
if($exampledate > $oldestdate)
$oldestdate = $exampledate;
Thanks
The nice thing about YYYY-MM-DD style dates is that they will always sort correctly, whether treated as text (as in your example), numbers (e.g. 20141012), or actual dates.
Thus, there's no need to do anything special to compare them as long as everything is the same format. Your code, as written, should work as-is (besides the typos for $youngestdate).
Note that if you want to do anything besides comparing them -- e.g. anything actually involving treating them like actual dates -- you will indeed want something like strtotime() or a mix of mktime() + substr()
have you tried strotime? reference http://php.net/manual/de/function.strtotime.php
then you can easily compare with <and > and so on.
have you tried checkdate(12, 31, 2000)? PHP.net Checkdate function
For years between 1 and 32767 inclusive.Check post 2 in the php.net link
You should use the DateTime class.
$arr = ['2012-10-12', '2004-10-12', '2014-08-12', '2014-09-12', '2014-09-13', '2014-09-11'];
$_now = new DateTime('now');
foreach ( $arr as $_t ) {
$d = new DateTime ( $_t );
if ( !isset($newest) || $d >= $newest ) $newest = $d;
if ( !isset($oldest ) || $d <= $oldest ) $oldest = $d;
}
echo 'Newest ' . $newest->format('Y-m-d');
echo 'Oldest' . $oldest->format('Y-m-d');
Take a look here: Reference on php.net
And here is an working example
I tried to create a customizable number according to a defined mask.
This is my rules to make a mask
You may enter any numbering mask. In this mask, the following tags could be used: {000000} corresponds to a number which will be incremented on each customer.
Enter as many zeros as the desired length of the counter.
The counter will be completed by zeros from the left in order to have as many zeros as the mask.
{000000+000} same as previous but an offset corresponding to the number to the right of the + sign is applied starting on first .
{000000#x} same as previous but the counter is reset to zero when month x is reached (x between 1 and 12).
If this option is used and x is 2 or higher, then sequence {yy}{mm} or {yyyy}{mm} is also required.
{dd} day (01 to 31).
{mm} month (01 to 12).
{yy}, {yyyy} or {y} year over 2, 4 or 1 numbers.
All other characters in the mask will remain intact.
Spaces are not allowed.
Example on customer created on 2007-03-01:
ABC{yy}{mm}-{000000} will give ABC0701-000099,
{0000+100}-ZZZ/{dd}/XXX will give 0199-ZZZ/31/XXX
So my current mask is C{000000}
<?php
$mask = "C{000000}";
$number = 100;
if (preg_match('/\{(0+)([#\+][0-9]+)?([#\+][0-9]+)?\}/i',$mask,$regType)){
$masktype=$regType[1];
$masktype_value=substr(preg_replace('/^TE_/','',$number),0,strlen($regType[1]));//get n first characters of code where n is length in mask
$masktype_value=str_pad($masktype_value,strlen($regType[1]),"#",STR_PAD_RIGHT);
$maskwithonlyymcode=$mask;
$maskwithonlyymcode=preg_replace('/\{(0+)([#\+][0-9]+)?([#\+][0-9]+)?\}/i',$regType[1],$maskwithonlyymcode);
$maskwithonlyymcode=preg_replace('/\{dd\}/i','dd',$maskwithonlyymcode);
$maskwithonlyymcode=preg_replace('/\{(c+)(0*)\}/i',$maskrefclient,$maskwithonlyymcode);
$maskwithonlyymcode=preg_replace('/\{(t+)\}/i',$masktype_value,$maskwithonlyymcode);
$maskwithnocode=$maskwithonlyymcode;
$maskwithnocode=preg_replace('/\{yyyy\}/i','yyyy',$maskwithnocode);
$maskwithnocode=preg_replace('/\{yy\}/i','yy',$maskwithnocode);
$maskwithnocode=preg_replace('/\{y\}/i','y',$maskwithnocode);
$maskwithnocode=preg_replace('/\{mm\}/i','mm',$maskwithnocode);
print "maskwithonlyymcode=".$maskwithonlyymcode." maskwithnocode=".$maskwithnocode."\n<br>";
}
?>
But it is not working it is printing
maskwithonlyymcode=C000000 maskwithnocode=C000000
My desired output is C000001 - C000100.
What is missing in this code?
I do not understand your code much, so I was not able to fix it, but what about:
<?
function process_mask($mask, $number, $date)
{
while (preg_match("/\{(.+?)\}/", $mask, $match))
{
$outter_code = $match[0];
$inner_code = $match[1];
if (preg_match("/^(0+)(\+(\d+))?$/", $inner_code, $match2))
{
$number2 = $number;
if (!empty($match2[3]))
{
$number2 += intval($match2[3]);
}
$replacement = str_pad($number2, strlen($match2[1]), "0", STR_PAD_LEFT);
}
else
{
switch ($inner_code)
{
case "dd":
$replacement = date("d", $date);
break;
case "mm":
$replacement = date("m", $date);
break;
case "y":
$replacement = substr(date("Y", $date), 3);
break;
case "yy":
$replacement = date("y", $date);
break;
case "yyyy":
$replacement = date("Y", $date);
break;
default:
trigger_error("Unrecognised code $inner_code");
return NULL;
}
}
$mask = str_replace($outter_code, $replacement, $mask);
}
return $mask;
}
function test_mask($mask)
{
$date = mktime(0, 0, 0, 4, 19, 2013);
echo str_pad($mask, 25)." => ".process_mask($mask, 100, $date)."\n";
}
test_mask("C{000}");
test_mask("C{000000}");
test_mask("C{000000+10}");
test_mask("ABC{yy}{mm}-{000000}");
test_mask("{0000+100}-ZZZ/{dd}/XXX");
?>
Outputs:
C{000} => C100
C{000000} => C000100
C{000000+10} => C000110
ABC{yy}{mm}-{000000} => ABC1304-000100
{0000+100}-ZZZ/{dd}/XXX => 0200-ZZZ/19/XXX
I absolutely do not undertand your rules about resetting counters. Based on what date do you want to reset the numbers? Current date? Do you keep some counter per customer (you have not explained what the number is)? Why resetting it on certain month? Wouldn't it be more meaningful to reset it in intervals? Like every month (implementation-wise, it would make sense then to keep separate counter for every month, so that the formatting logic is current time-independent). Some example may help understanding this.
Also for date formatting, I would suggest you to stick with PHP date formatting and do not invent your own.
I would suggest you to use pattern like this instead (It's actually bit .NET-like):
{(#[+offset]|php-date-format-string)[:length]}
So (for number = 999 and date = 2013-04-19):
C{#:4} => C0999
C{#+10:4} => C1009
C{#:6} => C000999
C{#:4}/{Y} => C0999/2013
C{#:4}/{Y:4} => C0999/2013
C{#:4}/{Y:2} => C0999/13
C{#:4}/{Y:1} => C0999/3
C{#:4}/{m} => C0999/03
C{#:4}/{Y}{m} => C0999/201303
C{#:4}/{Ym} => C0999/201303
Code for this would be way simpler, more extensible and flexible.
I've currently got 2 dates in PHP - a 'start' date and an 'end' date. I have then created an array of dates, using a function I found called createDateRangeArray ($date_range). For simplicity, the end date will always be today's date.
I've also got a separate array that contains a bunch of dates ($valid_dates) that will always fall between the start and end dates mentioned above. On those dates, 'something' happened - in this case, a training session.
I'm trying to get my head around getting the following:
A range of dates similar to the $date_range array, populated with TRUE or FALSE based on whether or not a training session happened on that date. I'm happy for this to be an assoc array with keys named date and, say, session_found (bool).
The longest 'streak' i.e. the longest consecutive number of days that training sessions occurred.
The longest 'slump' i.e. the longest consecutive number of days that training sessions did NOT occur.
Can someone point me in the right direction for getting the above info without using a foreach on the contents of the $date_range array and then having to use another foreach for the $valid_dates array on each item in the $date_range array? That is horribly inefficient ...
Sorry if I've over-complicated things with all that info but any help would be much appreciated.
I'm currently using PHP 5.4 on Debian Wheezy, if that helps (typical LAMP stack).
Thanks
This is completely untested, but how about something like the following:
You should get the end date of the longest streak and slump, as well as how many days it took. $sessions will contain an array with the dates as the keys, and true for days with sessions, and false for days without.
Still a foreach, but I dont think you can avoid using one. Let me know if it works, I really dont have any idea how well this code will behave, but hopefully will give you a starting point?
$streak = 0;
$slump = 0;
$longeststreak = 0;
$longestslump = 0;
$longeststreakend = 0;
$longestslumpend = 0;
foreach ($date_range as $date) {
if (in_array($date, $valid_date)) {
$sessions[$date] = true;
$streak++;
// Slump broken, record the length if it beat the previous record
if ($longestslump < $slump) {
$longestslump = $slump;
$longestslumpend = $date;
}
$slump=0;
}
else {
$sessions[$date] = false;
$slump++;
// Streak broken, record the length if it beat the previous record
if ($longeststreak < $streak) {
$longeststreak = $streak;
$longeststreakend = $date;
}
$streak=0;
}
}
The following script issues a
'Warning: range() [function.range]: step exceeds the specified range in'
only when the date_diff function is called. Does anyone know why?
<?php
$array = array(
"Interno",
"id"
);
$step = count($array) - 1;
foreach (range(0, $step) as $number) {
echo '<p>'.$number.'</p>';
}
$datetime1 = new DateTime('2010-08-2');
$datetime2 = new DateTime('2009-07-30');
$interval = date_diff($datetime1,$datetime2);
?>
Well, the two functions have nothing to do with each other.
Secondly, the second parameter to range is not a step, it's a maximum value (see the range docs... So if you're getting a step exceeds the specified range error, I'd guess that the default step value 1 is larger than the max of the range (the result of count($array) - 1)... I'm not sure why that's happening in your code, but it's a start
That's a PHP bug.
Still present as of 5.3.5, it seems that was fixed in 5.3.6.
https://bugs.php.net/bug.php?id=51894
I do agree with ircmaxell, function range and date_diff are not related and do not interact in any way. The issue should be in your array which get altered in some way.
Also, as for me, your example contain unnecessary operations like count and range and it could be shortened to this:
<?php
$array = array(
"Interno",
"id"
);
foreach ($array as $number => $value) {
echo '<p>'.$number.'</p>';
}
$datetime1 = new DateTime('2010-08-2');
$datetime2 = new DateTime('2009-07-30');
$interval = date_diff($datetime1,$datetime2);
?>