Undefined offset, but array items exists [duplicate] - php

This question already has answers here:
Getting rid of PHP Notice: Undefined offset [duplicate]
(11 answers)
Closed 9 years ago.
I have problem which i can't understand.
I get error Undefined offset with this code:
$date = $this->date;
$dateArr = explode('-', $date);
$newDate = $dateArr[2] . '.' . $dateArr[1] . '.' . $dateArr[0];
$this->date is a string like "2013-11-10"
var_dump($dateArr) says, that there are these values in the array, same as Debugger.
I dont understand why do i get this error. This is only a "notice", but it makes me angry.
What is funny the date is saved to the database anyways, and this is correct (like $newDate is built correctly).
-- edit
By doing this i want to convert the date from yyyy-mm-dd to dd-mm-yyyy, then convert this to unix timestamp.

If you want to get "10.11.2013" as a result - you'll get it (your code works OK), but only in case $this->date is set and that is really string like "2013-11-10" (I'm sure it is not). In all other cases when your explode will fail and will return different result (array that does not have 3 elements) - you'll get offset.

Assuming $this->date is actually set, there is no need for your multi manipulations just use strtotime() to get the timestamp and then date() to format.
// To change formatting to 10.11.2013
$newDate = date("d.m.Y", strtotime($this->date));
// To turn date into unix timestamp 1384038000
$newDate = strtotime($this->date);

Related

php date & strtotime function resulting in 1970-00-00 00:00 value [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
As ive echoed out the datetime value from my db, i am now trying to display this value to edit it in the datetime-local field within my form.
The datetime vaue from db is set to:
22/3/2017 10:00:00
however, after attempting to use the following code, im left with this:
1970-01-01T01:00:00
$dat = date("Y-m-d\TH:i:s", strtotime($_GET["dat"]));
How & why is this function not working correctly to display '22/3/2017 10:00' in the form field?
Use DateTime::createFromFormat:
$date = DateTime::createFromFormat('d/m/Y H:i:s', '22/3/2017 10:00:00');
$dat = $date->format('Y-m-d\TH:i:s');
echo $dat;
Your code is not working because strtotime makes assumption based on delimiters about actual format:
m/d/Y- American format
d.m.Y or d-m-Y - European
It's not working because strtotime() can translate only specific date format.
Check the manual. For a list of supported date format, look here.
Your date format looks not included in the supported ones to me.
Examples:
strtotime("03/22/2017 10:00:00"); // Works: returns 1490173200
strtotime("22/3/2017 10:00"); // Doesn't work: returns false
You have to either change the date format in your DB or format it to one of the supported formats to make it work.

PHP convert a MM/DD/YYYY string to a MMDDYYYY [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a string representing a date. It's format is MM/DD/YYYY. I need to submit it via API to a service that requires MMDDYYYY. Should I bother with overloading this string onto some date class and 'export' as MMDDYYY or just delete the "/" sub-string wherever I find it?
Any other neat way of doing that I wasn't aware of?
Since you just need to remove the '/' I would do that with the following code, it will eliminate the overhead of parsing the date.
$newDate = str_replace ('/', '', $oldDate);
You can use the DateTime object in PHP (http://php.net/manual/en/class.datetime.php)
Here is how I would do it:
<?php
$myDate = '05/15/2015';
$date = DateTime::createFromFormat('m/d/Y H:i:s', "$myDate 00:00:00");
$newDate = $date->format('mdY');
echo $newDate . PHP_EOL;
?>

Date format conversion results wrong date [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I tried converting
12-18-1997
to
18-12-1997
with this code
$new_date = date('d-m-Y', strtotime('12-18-1997'));
but it results in 18-12-1969
If I have to convert full date alongwith time then its converting fine but in the date I posted in question there is no time.
Use DateTime instead of strtotime():
$date = DateTime::createFromFormat( 'm-d-Y', '12-18-1997');
echo $date->format( 'd-m-Y');
You can see from this demo that it prints:
18-12-1997
strtotime is good, but it's not psychic or omniscient. you're feeding it a time string it's not able to parse properly:
php > var_dump(strtotime('12-18-1997'));
bool(false)
Since you simply assumed it's succeeding, you feed that false back to date(), where it's type-cast to an integer 0. However, your result is impossible, since int 0 as a date is Jan 1/1970. With timezone conversions, it'd be 31-12-1969 for you, NOT 18-12.
If you can't feed strtotime a format it understands, then use date_create_from_format and TELL it what what the format is:
$date = date_create_from_format('m-d-Y', '12-18-1997');
$text = date('d-m-Y', $date);

PHP datetime add one second [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP date time
Trying to add one second to a datetime that is input by the user
$values['start_date_'.$j.'-'.$i] is a valid datetime string, however the following code is throwing an error
$priceStart = date('Y-m-d H:i:s',strtotime($values['start_date_'.$j.'-'.$i]));
date_modify($priceStart, '+1 second');
$priceStart =date_format($priceStart, 'Y-m-d H:i:s');
The error is "date_modify() expects parameter 1 to be DateTime, string given in... on line..."
same error follows for date_format()
what is the correct syntax for this?
Use a DateTime object instead. It's much more powerful and easy for this one.
$priceStart = new DateTime("#" . strtotime($values['start_date_'.$j.'-'.$i]));
$priceStart->modify("+1 second"); //You're pretty much done here!
echo $priceStart->format("Y-m-d H:i:s"); //Just to see the result.
date() gives you a string. date_modify needs a DateTime object.
The easiest way to do what you want is simply adding one to the value returned by strtotime():
$priceStart = date('Y-m-d H:i:s',strtotime($values['start_date_'.$j.'-'.$i]) + 1);
Or, you can create a DateTime object:
$priceStart = new DateTime('#' . strtotime($values['start_date_'.$j.'-'.$i]));
and the rest of your code should start working.

How to find date differences in php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP calculate person's current age
I want to find the persons age.
$time=$row['date_of_birth'];
$date=explode("-", $time);//gets the date birth from the database and puts it into an array
$a=getdate();//gets todays date
$diff_date= gregoriantojd($a["mon"],a["mday"],a["year"] ) - gregoriantojd(date[1],date[2],date[0]);//subtracts the differences between the dates, this is returned in seconds..
The question is whether this is the way to do it? and how do I convert the seconds and convert them to years (as an integer)?!
EDIT:
I think that the last line should be this:
return floor($diff_date /60*60*24*365);
Just try with strtotime (converts date string into timestamp):
$time = strtotime($row['date_of_birth']);
$age = date('Y', $time - time());
echo 'age: ' . $age;
If $time is supposed to be unix timestamp
$time=$row['date_of_birth'];
$date1 = new DateTime($time);
$date_diff = $date1->diff(new DateTime(), true);
$age = $date_diff->y;
I think you can make use of the date_diff function in PHP.
1st question: The question is whether this is the way to do it?
No. The function gregoriantojd (per the manual entry here) returns a value in days, not seconds. I like the date_diff conversion that Piotr gives the best.
2nd question: how do I convert the seconds and convert them to years
(as an integer)?!
Well, if you were starting with a number of seconds (which you aren't) then your second formula would be mostly correct, but of course it wouldn't account for leap-years. So for anyone older than 4, or at least 8, it would be off for one or more days around their birthday. Usually on their birthday is a pretty important time to get their age correct.

Categories