Extracting Usable Info From datetime field in php/MySQL - php

I am trying to format a datetime variable with the following code:
$passed_time = $stu_quiz->c_date_time;
$passed_time_string = date_format($passed_time, 'M-d-Y');
For some reason, if I print $passed_time_string, the output is blank, but if I print out $passed_time, I get the date (in the format 2011-06-15 21:43:09).
Why is the date_format method not working?

The date_format function expects a "DateTime" object that is created using date_create.
Example:
$passed_time = date_create($stu_quiz->c_date_time);
$passed_time_string = date_format($passed_time, 'M-d-Y');

You are looking for just date()
http://php.net/manual/en/function.date.php

If you don't have PHP 5.3 and cannot use the DateTime class, try this:
$passed_time_string = date("M-d-Y", strtotime($passed_time));
First it converts your original MySQL time to a unix timestamp, then formats it as M-d-Y

Related

Convert number to date in PHP and format to YYYYMMDD [duplicate]

This question already has answers here:
PHP remove dots and hyphens
(5 answers)
Closed 3 years ago.
I want to convert date from, for example, 1441065600 to YYYYMMDD format. How do I do that?
$temp = date("Y-m-d",1441065600);
$rel_date = date_format( $temp, "YYYYMMDD");
The code above gives an error:
date_format() expects parameter 1 to be DateTimeInterface
Using date()
Simply write:
<?php
echo date('Ymd', 1441065600);
Otherwise, using a DateTime instance:
Short
<?php
echo (new DateTime())->setTimestamp(1441065600)->format('Ymd');
Note: the setTimestamp() method do not takes a string as parameter! Otherwise, you may want to do so:
Long
<?php
$english = '1441065600';
$timestamp = strtotime($english);
$date = new DateTime($timestamp);
echo $date->format('Ymd'); // 20161214
Descriptions
strtotime() - Parse about any English textual datetime description into a Unix timestamp
DateTime - Representation of date and time
Note: I created a new DateTime instance from the UNIX timestamp, English textual representation may lead to an error.
Other formats
I recommend you to read the DateTime::format() method documentation along with the date() function documentation to learn more about date formats.
$date = (new DateTime())->setTimestamp(1441065600);
echo $date->format('Ymd');
date_format is function in which first argument should be DateTime object, date function return string.
So first You need to create correct object.
$date = new DateTime(strtotime(1441065600));
and then format it with date_format
echo date_format($date,'Ydm');
You can pass the timestamp to the DateTime constructor by prepending # character:
$d = new DateTime('#1441065600');
echo $d->format("Ymd");
See Compound Formats.
Use Date function in PHP
echo $temp = date("Y-m-d",1441065600);
Result:
2015-09-01
Refer http://php.net/manual/en/function.date.php

DateTime function unix timestamp converting wrong

I am using DateTime function of php. I get a date from a calendar in format d-m-Y and pass it via ajax to my function. I am getting the date right till this step.
When I try to store the date in unix format using:
$ai_ff_date=DateTime::CreateFromFormat('d-m-Y', $data['date']);
$final_date=$ai_ff_date->format('U');
The date stored is wrong. Suppose the date I passed via ajax is 26-12-2016 then in database 27-12-2016 is stored. Why its counting one more day then the input.
use this code :
$date = date('Y-m-d H:i:s', strtotime('-1 day', $stop_date));
$ai_ff_date=DateTime::CreateFromFormat('d-m-Y',$date);
$final_date=$ai_ff_date->format('U');
and please check the variable (code not tested)
You might want to convert the Date-Format to "Y-m-d" First and then call-in the DateTime() Constructor. However, since what you are trying to do is just get the TimeStamp you might also do that directly without using DateTime. The Snippet below shows what is meant here:
<?php
$data = ['date'=>"13-12-2016"]; //<== JUST AN EXAMPLE FOR TESTING!!!
// SIMPLY CONVERT THE DATE TO Y-m-d FIRST.
$dateYMD = date("Y-m-d", strtotime($data['date']));
// THEN USE DateTime CONSTRUCTOR TO CREATE A NEW DateTime INSTANCE
// AND THEN RUN THE FORMAT YOU WISH::
$final_date = (new DateTime($dateYMD))->format('U');
var_dump($final_date); //<== YIELDS: string '1481583600' (length=10)
var_dump(date("Y-m-d", $final_date)); //<== YIELDS: string '2016-12-13' (length=10)

strtotime () not converting to timestamp

I have a loop and in it the date is coming in different formats like for some values it will be like '10-13-2013 04:31' and for some it is like '2013-10-14T22:14:40-0700'. I tried to store this in DB as the value of a datetime/timestamp column but it is failing for the first format that is 10-13-2013 04:31. So I tried to convert it into UNIX timestamp using strtotime(). It is working for some values and is storing zero for values like '10-13-2013 04:31'. I think this is because it is considering the second value as month and so failing. My code is as follows :
foreach($reports as $report){
echo strtotime($report->transactionDate);
}
strtotime() is unable to parse mm-dd-yyyy format. Instead you should use DateTime::createFromFormat(), like this:
$date = '10-13-2013 04:31';
$obj = DateTime::createFromFormat('m-d-Y H:i', $date);
$date = $obj->format('Y-m-d H:i:s');

Format DATETIME from MYSQL database using PHP

So I have a field in my database called 'DateTime' and the following lines of code:
echo "Date/Time: ";
echo $row['DateTime'];
How do I format it so that instead of being like this:'2013-02-07 22:14:56', it will be like this: '07/02/13 - 22:14'
Thanks.
Alternatively you could use:
DateTime::createFromFormat('Y/m/d H:i:s',$row['DateTime']); this will give you a datetime object, which are quite nice to work with.
Another alternative would be to have MySQL format the DATETIME value as a string in the desired format, using the DATE_FORMAT function.
SELECT DATE_FORMAT(`DateTime`,'%d/%m/%y - %H:%i') AS `DateTime`
...
No change required to your PHP code except for the SQL text sent to the database server.
This approach can very efficient, and reduce the amount of code you need, if all you are doing with this string is displaying it. If you are doing any sort of manipulation on this value, then casting the string value returned from MySQL resultset into a datetime object is probably a better way to go.
A demonstration of the DATE_FORMAT function:
SELECT DATE_FORMAT('2013-02-07 22:14:56','%d/%m/%y - %H:%i') AS `DateTime`
DateTime
----------------
07/02/13 - 22:14
how to output date into Year textbox Month textbox Day textbox
$book_date = $myrow["Publication_Day"];
$book_year = Date("Y", strtotime($book_date));
$timestamp contains ur date & time in any format.....................
date('Y/m/d - H:i',strtotime($timeStamp));
echo date('d/m/y H:i', strtotime($row['DateTime']));
See date and strtotime for more detail on the functions from the docs
$mytime = strtotime('2013-06-07 22:14:56');
$newDate = date('m/d/y - G:i', $mytime);
echo $newDate;
Here's an alternative using DateTime. If you're working with timezones this code can be easily modified to handle that.
$datetime = new DateTime('2013-02-07 22:14:56');
echo $datetime->format('d/m/y H:i');
See it in action

Converting date to this format

I have a date in this format:
24-12-2010 // DAY - MONTH - YEAR
I need to get it in this format:
1995-12-31T23:59:59.999Z // The Z is for the TimeZone I think.
Check this link out:
http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html
The above link is the way I need the date.
I am using PHP now, so this needs to be with PHP.
How can I convert these dates the easiest way?
Thanks
That is an ISO8601 format date; the following is what you want.
gmdate('Y-m-d\TH:i:s\Z', strtotime($date_value));
You can do something like that:
$dateTime = new DateTime($myDate);
$formatted = $dateTime->format("Y-m-d\TH:i:s.z\Z");
The mentioned solution with:
$dateTime->format(DateTime::W3C);
$dateTime->format(DateTime::ISO8601);
does return strings like:
2012-11-28T17:21:11+0100
which cannot be parsed, at least with newer Solr versions.
I wouldn't use gmdate if you need to support timezones. The DateTime implementation is well done, and is also available for functional programming.
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/ref.datetime.php
You can use the DateTime class
$dateTime = new DateTime();
$dateTime.setDate(24, 12, 2010);
$output = $dateTime.format(DateTime::W3C);
// Output now is your date in W3C format.
use the date ( string $format [, int $timestamp ] ) function of php!
In second paramter use http://php.net/manual/en/function.strtotime.php to get the timestamp from strings
$date = strtotime('24-12-2010');
$new_date = gmDate("Y-m-d\TH:i:s.z\Z",$date);

Categories