How to: parse a string 'mm/yyyy' into a date in PHP5.3? - parsing

I come from Java & C# worlds, and just wondering how do I parse a string formatted as mm/yyyy into a date in PHP 5.3.
I've tried with the following:
date_parse_from_format('mm/yyyy', '05/2013');
Then the returned array complains with errors:
[2] => Unexpected data found.
[5] => The separation symbol could not be found
[7] => Data missing
How to parse to date a string formatted as mm/yyyy in PHP 5.3?
Here is the complete var_dump:
Array
(
[year] => 2013
[month] => 20
[day] =>
[hour] =>
[minute] =>
[second] =>
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 3
[errors] => Array
(
[2] => Unexpected data found.
[5] => The separation symbol could not be found
[7] => Data missing
)
[is_localtime] =>
)

Use 'm/Y' instead of 'mm/yyyy'. Look at the date() function for details.
date_parse_from_format('m/Y', '05/2013');
What to do next...first of all I'd use Object oriented style:
$date = DateTime::createFromFormat('m/Y', '05/2013');
// 2013-05
echo $date->format('Y-m');
// 1369946144 UNIX timestamp
echo $timestamp = $date->format('U');
// 2013-05 using date(), procedural style
echo date('Y-m', $timestamp );

Related

Getting Google Ads API keyword_plan_idea_error: The input has an invalid value

$requestOptionalArgs = [];
$requestOptionalArgs['keywordSeed'] = new KeywordSeed(['keywords' => $keywords]);
$keywordPlanIdeaServiceClient->generateKeywordIdeas([
'language' => ResourceNames::forLanguageConstant(1000), // English
'customerId' => $customerId,
'geoTargetConstants' => $geoTargetConstants,
'keywordPlanNetwork' => KeywordPlanNetwork::GOOGLE_SEARCH
] + $requestOptionalArgs);
The above code is working fine if the $keywords array size is not more than 20. If I add the 21st keyword to the $keywords array then it's throwing the below error.
keyword_plan_idea_error: The input has an invalid value.

Index in foreach starts not with 0

$rounds = $season->championsLeague->rounds->where('stage', 'Olympic')->take(2);
$indexes = [];
foreach ($rounds as $index => $round) {
$indexes[] = $index;
}
echo '<pre>';print_r($indexes);echo '<pre>';
And I receive in indexes: Array
(
[0] => 6
[1] => 7
)
How it is possible?
Why not Array
(
[0] => 0
[1] => 1
)
The slice, chunk, and reverse methods now preserve keys on the collection. If you do not want these methods to preserve keys, use the values method on the Collection instance. This is from laravel documentation. I think it is true for take method also.

Extract values from a string in Ruby

How to get a value from a string e.g
search_params[:price] = "1460,4500"
How can I get the first number into one variable and second into a different variable?
Did you mean this??:
first_price, second_price = search_params[:price].split(',')
You can use split method
irb(main):002:0> price = "1460,4500"
=> "1460,4500"
irb(main):003:0> price.split(',')
=> ["1460", "4500"]
irb(main):004:0> a, b = price.split(',')
=> ["1460", "4500"]
irb(main):005:0> a
=> "1460"
irb(main):006:0> b
=> "4500"

Neo4j ResultSet Object - how to get data if result is an array

I have one gremlin query in which I used cap().next()
and Everyman\Neo4j\Query\ResultSet Object is
...
[data:protected] => Array
(
[v[1079]] => Array
(
[0] => 14
)
[v[1082]] => Array
(
[0] => 25
)
[v[1016]] => Array
(
[0] => 5
)
[v[1078]] => Array
(
[0] => 10
)
[v[1081]] => Array
(
[0] => 17
)
)
...
how to get that array?
$result[0][0] is not working.
To iterate ResultSets use
foreach ($result as $row) {
echo $row['x']->getProperty('your_property') . "\n";
}
or with scalar values in column y
foreach ($result as $row) {
echo $row['x']->getProperty('your_property') . ": " . $row['y'] ."\n";
}
It would be nice to have the original gremlin query thought to see what you are returning from it.
see github

Take array and convert to a hash Ruby

I am trying this for the first time and am not sure I have quite achieved what i want to. I am pulling in data via a screen scrape as arrays and want to put them into a hash.
I have a model with columns :home_team and :away_team and would like to post the data captured via the screen scrape to these
I was hoping someone could quickly run this in a rb file
require 'open-uri'
require 'nokogiri'
FIXTURE_URL = "http://www.bbc.co.uk/sport/football/premier-league/fixtures"
doc = Nokogiri::HTML(open(FIXTURE_URL))
home_team = doc.css(".team-home.teams").map {|team| team.text.strip}
away_team = doc.css(".team-away.teams").map {|team| team.text.strip}
team_clean = Hash[:home_team => home_team, :away_team => away_team]
puts team_clean.inspect
and advise if this is actually a hash as it seems to be an array as i cant see the hash name being outputted. i would of expected something like this
{"team_clean"=>[{:home_team => "Man Utd", "Chelsea", "Liverpool"},
{:away_team => "Swansea", "Cardiff"}]}
any help appreciated
You actually get a Hash back. But it looks different from the one you expected. You expect a Hash inside a Hash.
Some examples to clarify:
hash = {}
hash.class
=> Hash
hash = { home_team: [], away_team: [] }
hash.class
=> Hash
hash[:home_team].class
=> Array
hash = { hash: { home_team: [], away_team: [] } }
hash.class
=> Hash
hash[:hash].class
=> Hash
hash[:hash][:home_team].class
=> Array
The "Hash name" as you call it, is never "outputed". A Hash is basically a Array with a different index. To clarify this a bit:
hash = { 0 => "A", 1 => "B" }
array = ["A", "B"]
hash[0]
=> "A"
array[0]
=> "A"
hash[1]
=> "B"
array[1]
=> "B"
Basically with a Hash you additionally define, how and where to find the values by defining the key explicitly, while an array always stores it with a numerical index.
here is the solution
team_clean = Hash[:team_clean => [Hash[:home_team => home_team,:away_team => away_team]]]

Resources