I have an array with time in PHP:
[time] => Array (
[year] => 2007
[month] => 1
[day] => 2
[hour] => 1
[minute] => 1 )
)
i must save this in MySQL. in MySQL i have format
2007-01-02 01:01
how is the best method for modify array with time to timestamp?
sprintf("%04d-%02d-%02d %02d:%02d", $arr['time']['year'], $arr['time']['month'], $arr['time']['day'], $arr['time']['hour'], $arr['time']['minute']);
Related
API Server responds with token expiration date in the following format:
2022-05-09T02:11:27.747
What format is it?
That's ISO-8601 standard time format. Year month day T hour minute second millisecond. The date_parse function will handle this.
<?php
$x = '2021-04-01T19:18:17.654';
print_r(date_parse($x));
?>
Output:
Array
(
[year] => 2021
[month] => 4
[day] => 1
[hour] => 19
[minute] => 18
[second] => 17
[fraction] => 0.654
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)
I don't understand why this is a valid date
strtotime("1920-09k-12") // -1556877600 // I expect false
I see that adding a char after the month or day is a valid date.
strtotime("1920-09-12d") // -1555905600 // I expect false
strtotime("1920-09n-12") // -1556838000 // I expect false
Instead
strtotime("1920-09k-12k") // false
strtotime("1920r-09-12") // false
strtotime("1920-09-12") // -1555862400
Is this the expected behaviour?
I use Laravel and the strtotime function is used for validate date in the framework but when go to save a record in DB with a "false positive" date a QueryException is raised.
I resolved the problem with a custom validation but i'm curious to know why strtotime has this behaviour.
This may not fully answer your question, but examining the results of date_parse() for your sample dates, it appears that the first letter in the string is interpreted as the timezone, and the remainder of the string as well, which either causes a warning or an error of "Double timezone specification", and in the case of an error, the DateTime cannot be created.
For example:
date_parse("1920-09-12d")
Array
(
[year] => 1920
[month] => 9
[day] => 12
[hour] =>
[minute] =>
[second] =>
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] => 1
[zone_type] => 2
[zone] => -240
[is_dst] =>
[tz_abbr] => D
)
Notice the timezone "D"
date_parse("1920-09n-12")
Array
(
[year] => 1920
[month] => 9
[day] => 1
[hour] =>
[minute] =>
[second] =>
[fraction] =>
[warning_count] => 1
[warnings] => Array
(
[8] => Double timezone specification
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] => 1
[zone_type] => 2
[zone] => 60
[is_dst] =>
[tz_abbr] => N
)
Notice the timezone "N", furthermore the 12 is not interpreted as the day of month, but rather I suspect "-12" is interpreted as an additional timezone specification, hence the warning.
date_parse("1920r-09-12")
Array
(
[year] =>
[month] =>
[day] =>
[hour] => 19
[minute] => 20
[second] => 0
[fraction] =>
[warning_count] => 1
[warnings] => Array
(
[5] => Double timezone specification
)
[error_count] => 1
[errors] => Array
(
[8] => Double timezone specification
)
[is_localtime] => 1
[zone_type] => 2
[zone] => 300
[is_dst] =>
[tz_abbr] => R
)
Notice the timezone "R", furthermore no date is parsed, rather 1920 is interpreted as the time 19:20:00, and I suspect the remainder of the string is interpreted as 2 timezones, "-09" & "-12", causing the error "Double timezone specification".
I'm running through all of the array functions on php.net and unable to figure this out.
Essentially I want to take these two arrays:
Array
(
[0] => stdClass Object
(
[month] => October
[year] => 2015
[credit] => 1000.00
)
[1] => stdClass Object
(
[month] => September
[year] => 2015
[credit] => 200.00
)
)
Array
(
[0] => stdClass Object
(
[month] => October
[year] => 2015
[debit] => 2000.00
)
[1] => stdClass Object
(
[month] => August
[year] => 2015
[debit] => 50.00
)
)
...and have the output look like this:
Array
(
[0] => stdClass Object
(
[month] => October
[year] => 2015
[credit] => 1000.00
[debit] => 2000.00
)
[1] => stdClass Object
(
[month] => September
[year] => 2015
[credit] => 200.00
[debit] => 0
)
[2] => stdClass Object
(
[month] => August
[year] => 2015
[credit] => 0
[debit] => 50.00
)
)
I'm looked to merge "month" and "year" and combine the other keys, using a default value if the key doesn't exist. Any guidance?
Assuming $debits and $credits are the arrays shown in your question, I would approach it like this:
First loop over the credits, inserting them into the new "combined" array and adding a default value for debit as you go.
foreach ($credits as $credit) {
$credit->debit = 0.00; // provide a default value for debit
$combined[$credit->year . $credit->month] = $credit;
}
Then loop over the debits. Since there is the possibilities that entries will already be there from credits, there needs to be a check for this. This part should update existing values inserted from credits, or insert new values if there is no existing value.
foreach ($debits as $debit) {
if (isset($combined[$debit->year . $debit->month])) {
// update the debit if the entry already exists
$combined[$debit->year . $debit->month]->debit = $debit->debit;
} else {
// otherwise create a new entry with a default value for credit
$debit->credit = 0.00;
$combined[$debit->year . $debit->month] = $debit;
}
}
// If you need the results to be numerically indexed, you can use array_values
$numerically_indexed = array_values($combined);
using cakePhp for an existing product they have old version 1.2.
I just started using CakePhp.
I tried to save some data to the table.
here is my code.
function add(){
//echo $id;
pr($this->data);
if(!empty($this->data)){
$this->User->create();
if($this->User->save($this->data)){
debug($this->User->validationErrors);
exit();
$this->Session->setFlash("Data saved man");
$this->redirect(array('action'=>'index',2));
}
}
}
Below is debug data.
Array
(
[user] => Array
(
[first_name] => hello
[last_name] => man
[email] => lrer#drei.cm
[password] => 123
[created_at] => Array
(
[month] => 01
[day] => 17
[year] => 2014
[hour] => 08
[min] => 56
[meridian] => am
)
[modified_at] => Array
(
[month] => 01
[day] => 17
[year] => 2014
[hour] => 08
[min] => 56
[meridian] => am
)
)
)
app\controllers\users_controller.php (line 36)
Array
(
)
(default) 0 query took ms
Nr Query Error Affected Num. rows Took (ms).
I have no clue why data is not saving to database. I need to get it work for version 1.2. thanks
I think user key should have capital U as User. Check your view code to make sure that the model passed to $form->create('User') not 'user'
I am having troubling using foreach with an array of associative arrays, where the keys in the associative arrays are numbers.
$rows = $_POST["row"];
// print_r($rows);
foreach ($rows as $r) {
fwrite($f, $r["date"]);
fwrite($f, "#");
fwrite($f, $r["desc-short"]);
fwrite($f, "#");
// etc.
}
The POST variable contains arrays identified by row[index]. If I stick in a print_r() it displays the POST values in the order they appeared in the original form (which is not necessarily numerical order, as rows can be inserted in the middle and the counter represents when they were added, not where), but when I iterate with foreach it ends up printing row[8] (assuming eight rows) last, even though it was inserted after row 2 (for example).
It seems that because my keys are numbers, foreach is treating the keys as if they were the order. How can I avoid this behavior?
Output of example data from print_r($rows):
Array ( [1] => Array ( [date] => 12/12/2013 [desc-short] => Show title [desc-long] => A sample long description [start-time] => 12:30 [duration] => 13 [rating] => TVY ) [2] => Array ( [date] => 12/12/2013 [desc-short] => TEST [desc-long] => TEST [start-time] => 12:45 [duration] => 14 [rating] => TVY ) [8] => Array ( [date] => 12/12/2013 [desc-short] => Calendar of Events [desc-long] => A list of local events displayed every hour on the hour [start-time] => 13:00 [duration] => 15 [rating] => TVY ) [3] => Array ( [date] => 12/12/2013 [desc-short] => Show title [desc-long] => A sample long description [start-time] => 12:45 [duration] => 12 [rating] => TVY ) [4] => Array ( [date] => 12/12/2013 [desc-short] => Calendar of Events [desc-long] => A list of local events displayed every hour on the hour [start-time] => 13:00 [duration] => 15 [rating] => TVY ) [5] => Array ( [date] => 12/12/2013 [desc-short] => Show title [desc-long] => test [start-time] => 13:15 [duration] => 100 [rating] => TVY ) [6] => Array ( [date] => 12/12/2013 [desc-short] => Calendar of Events [desc-long] => A list of local events displayed every hour on the hour [start-time] => 15:00 [duration] => 15 [rating] => TVY ) )
And yes, 7 is missing. I'll have to look into that as well. Rows number 1-6 were loaded from a file, while [8] was added in their midst later with JavaScript.
You can't avoid this if your keys are stuck like this. That's just how PHP works. If you need to maintain the order then you'll need to prefix numbers with letter