PHP DateTime() date issue [duplicate] - php

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

Related

convert a date string using PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I need to convert a date string in a format like bellow using PHP,
2015-07-30T08:05:00.917Z
I try to convert using the bellow code,
echo str_replace('+00:00', '.917Z', gmdate('c', strtotime('2016-04-05 00:00:00')));
This will create 2016-04-05T00:00:00.917Z This is not correct,Is there any function to create date like this format?
There are two ways to create date from String.
1. date_create_from_format
$date=date_create_from_format("DATE_FORMAT",$dateString);
2. $date=new DateTime();
$date=$date->createFromFormat("DATE_FORMAT", $dateString);
Using following link to get your DATE_FORMAT.
http://php.net/manual/en/function.date.php
You can do it as #Alok has mentioned in his answer.
Or similarly using the following code.
$date = DateTime::createFromFormat('U.u', microtime(TRUE));
echo $date->format('Y-m-d\TH:i:s.u\Z');
Note that the microsecond mentioned in your question is of 3 digits. But generally php date formatting outputs microseconds in 6 digits precision.
Like the above code outputs 2016-04-06T09:24:50.830000Z
So I think you have to do some hack there if the digit precision is important.
Pass it through the function formatTime. This creates a new DateTime object in PHP called $date that can also easily be manipulated if you need. The examples below formats the date in two different ways, both work.
Procedural Style:
function formatTime($millis) {
$date = new DateTime($millis);
return date_format($date, 'l, jS F Y \a\t g:ia');
}
Object Oriented Style:
function formatTime($millis) {
$date = new DateTime($millis);
return $date->format('l, jS F Y \a\t g:ia');
}
The format for date can be found at this link:
http://php.net/manual/en/function.date.php
Hope this helps.

Get current date as mm/dd/yyyy in php using strftime() because setlocale() is used [duplicate]

This question already has answers here:
How do I create a variable in PHP of today's date of MM/DD/YYYY format?
(6 answers)
Closed 8 years ago.
I am using:
strftime("%m/%d/%Y", date())
But it gives:
12/31/1969
I want current date like mm/dd/yyyy
Edit:
Can I get current date using strftime? because the code has setlocale() used
If you want such format:
echo strftime("%m/%d/%Y"); // 06/07/2014
or the date Function
echo date('m/d/Y'); // 06/07/2014
or the DateTime Class
$date = new DateTime();
echo $date->format('m/d/Y'); // 06/07/2014
You can use date like this
$data = date('m/d/Y');
You were attempting to use mysql-style date formatting. As you observed, that didn't work.

Reformat datetime variable in PHP [duplicate]

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!

Convert DateTime class to string [duplicate]

This question already has answers here:
Object of class DateTime could not be converted to string
(8 answers)
Closed 9 years ago.
Currently I am retrieving a date from mssql with the data type of small date time.
The data is : 2013-03-12 00:00:00
I want to store it in a variable and then display on a text box.
And the format I want to display is just 2013-03-12 on the text box.
The message I get is:
catchable fatal error : Object of class DateTime could not be
converted to string.
Any idea?
in php you can simply use date_format($date, 'Y-m-d')
<?php
$date = date_create('2013-11-23 05:06:07');
echo date_format($date, 'Y-m-d');
?>
returns
2013-11-23
The following code will give you a date usable for SQL:
$dateTime->format(\DateTime::ISO8601);
-edit-
Actually, I just saw that you want it the other way around. In that case:
$dateTime->format('Y-m-d');
Use CONVERT:
SELECT CONVERT(VARCHAR(10), datefield, 121)
FROM tablename;

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.

Categories