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.
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have the following datetime 2014-02-05 17:12:48 stored in a php variable named $reportDb["reportDate"].
Now, It is in Y-m-d H:i:s format, I want it in d/m/Y H:i:s format. How can I set or reformat this variable?
I tried with datetime::createformat but It doesn't works.
$formatted = date("d/m/Y H:i:s", strtotime($reportDb["reportDate"]))
If you want another method. Even if i would prefer the one already mentioned.
Output 05/02/2014 17:12:48
PHP 5.3 and older:
$dt = new DateTime('2014-02-05 17:12:48');
echo $dt->format(''d/m/Y H:i:s'');
PHP 5.4+:
$dt = (new DateTime('2014-02-05 17:12:48'))->format(''d/m/Y H:i:s'');
with the variable:
$dt = (new DateTime($reportDb["reportDate"]))->format(''d/m/Y H:i:s'');
If I understand you right, $reportDb["reportDate"] is already a DateTime object.
If so, all you need is ...
$reportDb["reportDate"]->format('d/m/Y H:i:s');
Cheers!
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);
This question already has answers here:
print_r() adds properties to DateTime objects [duplicate]
(7 answers)
Closed 9 years ago.
$now = new DateTime();
print_r($now);
print $now->date; // print the current date
BUT if print_r($now); is comment it show error ?
$now = new DateTime();
print $now->date; // Notice: Undefined property: DateTime::$date in
There is no such property in this class. Use format function instead:
echo $date->format('d.m.Y H:i:s');
http://www.php.net/manual/en/datetime.format.php
This is a bug in PHP (I 'm not sure exactly which versions are affected).
Class DateTime does not have a date property, but calling print_r on it creates a "hidden" property that looks like it's there (it is displayed with print_r) but in reality is not (you cannot get its value).
Instead of this, use DateTime::format to get the date value in whatever format you want.
When you want to print the date using the DateTime object, use the following method:
$Date = new DateTime();
$Date->format('d/m/Y H:i');
The following page can help you format the output:
http://php.net/manual/en/function.date.php
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Formating an SQL timestamp with PHP
I have a column in my SQL table that has default current timestamp values. I am trying to output it in a more readable format using date().
However, no matter which format I use, I always get the same date: 1970-01-01 00:33:32
Here is an example value of the current timestamp from the DB: 2012-08-09 06:37:58
Below is the code I use to try to "convert" it to a readable format:
$value['current_date']// is the var from the database.(containing 2012-08-09 06:37:58)
$somevar = date('Y-m-d H:i:s', $value['current_date']);
Thanks in advance.
Your problem is that you are using the wrong value in the second parameter. The date() function expects a UNIX-style timestamp as the second parameter, not a string representation of a date. Use the strtotime() function to correct this:
$somevar = date('Y-m-d H:i:s', strtotime($value['current_date']));
As someone else pointed out, however, why are you bothering to do this formatting? The format style you want is already in the database. It should be as easy as:
echo $value['current_date'];
The problem that you are having is because the second argument of the date() should be a timestamp, and since you are passing the raw string containing 2012-08-09 06:37:58, php does not know what to make of it.
Modify your code as below and it should work:
$somevar = date('Y-m-d H:i:s', strtotime($value['current_date']));
You can now use any date formats in place of 'Y-m-d H:i:s' as you wish
First of all: Why formatting the timestamp if it already exists in the desired format?
Regarding the actual problem:
See http://de2.php.net/manual/en/function.date.php
date() only accepts unix timestamps (seconds since 1970-1-1 00:00:00 UTC) or no timestamp at all. if you want to work with a timestamp like you have you need to create an unix timestamp first with date_create(), mktime , or strtotime
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Inverse date function? - not strtotime
Is it possible to get UNIX time from such date 2011-02-27 02:04:46?
hello,
we have this function to convert a timestamp to datetime:
$datetime = date('Y-m-d H:i:s', $timestamp);
is there a function to do the opposite?
datetime to timestamp.
Thanks
$timestamp = strtotime($datetime);
Or if you're confident of the format, split up the string with explode() or even substr and pass the necessary parts into the mktime function.
Be aware that strtotime can sometimes get the timestamp wrong, if a slightly unconventional format is used.
EDIT:
A really accurate way of doing this is if you know your input format, is to use DateTime::createFromFormat eg:
$dateTimeObject = \DateTime::createFromFormat('G:i', '9:30');
$dateTimeObject->format('H:i');
See http://php.net/manual/en/function.date.php for formatting guides, and http://php.net/manual/en/datetime.createfromformat.php for info on the method described above.
$timestamp = strtotime('12-04-2010');