datetime only returning date - php

I have a table with column date set to datetime. When i return and get the date from row it is only returning the date and not the time.
$date = $row["date"];
I have tried to format as below and I get the error of:
Warning: date_format() expects parameter 1 to be DateTime, string given
$date = date_format($row["date"], 'Y-m-d H:i:s');
How do I get the whole value?

in your select statement, cast the date into datetime. ex
SELECT CAST(date AS DATETIME) newDate
and retrieve it as
$dateTime = strtotime($row["newDate"]);

try:
$date = date_format(new DateTime($row['date']), "Y-m-d H:i:s");
OR
$date = date('Y-m-d H:i:s', strtotime($row['date']));

You need to convert your string $date to the right type. I had to try a few things to get this right, but this now behaves on my machine:
$thisDate = "2013-02-02 22:17:06"; // example you gave, as a string
$timezone="America/New_York"; // machine was complaining when I didn't specify
$DT = new DateTime($thisDate, new DateTimeZone($timezone)); // this really is a DateTime object
echo $DT->format('Y-m-d H:i'); // you can echo this to the output
$dateString = $DT->format('Y-m-d H:i:s'); // or format it into a string variable

You need to convert the string to date type first. Then date_format() will work. Try the following.
$date = date_format(date_create($row["date"]), 'Y-m-d H:i:s');
Good Luck

Related

Mysql format date to insert to table

I need to format and insert the date to mysql table which is type of datetime, I am doing this from php.
Here is the statement
$timestamp ="22-11-2015+00:00:00";
$mysqltime = date ("d-m-Y H:i:s", $timestamp);
But when I insert to table the date is wrong, it showing 31-12-1969 19:00:22
What could be the issue?
It should be
$timestamp ="22-11-2015+00:00:00";
$mysqltime = date ("d-m-Y H:i:s", strtotime($timestamp));
because date is expecting it's second parameter to be a number, specifically the number of seconds since midnight January 1st, 1970.
The date function expects an integer timestamp for the second argument.
This MSQL insert statement would work fine:
INSERT INTO mytable (birthday) ('2015-11-22 00:00:00')
If you're using a database class in PHP and the function you're calling is using a PHP date object, then you'd have to construct the PHP date object using the date() function.
Or if you want to use PHP code to reformat the date in PHP before sending the string to the database to be inserted, then I think you'd use strtodate to get a PHP date object, and then date()
$phpdate = strtodate('d-m-Y H:i:s', '22-11-2015 00:00:00')
$dateformysql = date('Y-m-d H:i:s', $phpdate)
databaseclass.dotheinsert($mysqldate)
This should be working, but did not work for me!
$timestamp ="22-11-2015+00:00:00";
$mysqltime = date ("d-m-Y H:i:s", strtotime($timestamp));
I believe it is because my national standard format (LC_TIME) is "Y-m-d" rather then "d-m-Y". However this works perfectly:
$timestamp ="22-11-2015+00:00:00";
// The '+' sign needs to be escaped!!
$time = date_create_from_format( "d-m-Y\+H:i:s", $timestamp);
$mysqltime = $time->format( 'd-m-Y H:i:s' );
On the other hand, MySQL use the date format "YYYY-MM-DD" as a standard. The correct format string should be:
$mysqltime = date ("Y-m-d H:i:s", strtotime($timestamp));
// or second option
$mysqltime = $time->format( 'Y-m-d H:i:s' );
I think the corret format would be:
$mysqltime = date_format ($timestamp,"d-m-Y H:i:s");
Let me know if this works

How to turn a string like "16/Sep/2014 08:34" to unix timestamp in PHP

Struggling without much success to turn strings like "16/Sep/2014 08:34" extracted from an array with explode command to unix timestamp like "2014-09-17 05:32:05" in PHP. Any help, please?
Edit: With #Erik's help I finally got the right result:
$date = DateTime::createFromFormat("d/M/Y H:i", $line[0]);
$date = $date->format('Y-M-d H:i');
$timestamp = strtotime($date);
You'll need to use DateTime::createFromFormat and then convert the resulting datetime to a timestamp by using $datetime->getTimestamp();
--
// this will create a generic PHP date object, which you can then manipulate into anything you want
$date = DateTime::createFromFormat( "d/M/Y H:i", "16/Sep/2014 08:34" );
// this will generate a unix timestamp (which is an integer)
$timestamp = $date->getTimestamp();
// this will generate the string you request in your question
$string = $date->format( "Y-m-d H:i:s" );
--
For more info on formatting dates, check out the PHP documentation: http://php.net/manual/en/datetime.createfromformat.php

select datetime with strtotime()

i want to select data which has a type datetime in mssql with php. I tried this code:
$date = strtotime($row['DateTime']);
echo date('Y-m-d H:i:s', $date);
it returns this error strtotime() expects parameter 1 to be string.
if i tried to convert string this DateTime it returns DateTime can not convert to string.
Can you explain what is wrong? Thanks
You already get a native PHP date object from the SQLSRV driver. You don't need to convert anything!
Whenever you need to print it, just use the DateTime::format() method:
echo $row['DateTime']->format('Y-m-d H:i:s');
try with datetime()
$date = new DateTime('2014-06-29 12:00:00'); //$row['DateTime']
echo $date->format( 'Y-m-d H:i:s');
or use directly format your date row
For more read manual :- http://www.php.net/manual/en/class.datetime.php
or may be it's already converted in correct date format so try
echo date('Y-m-d H:i:s', $row['DateTime']);

Add days to a timestamp

Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();

Getting time and date from timestamp with php

In my database I have a time stamp column...which reflects a format like this: 2012-04-02 02:57:54
However I would like to separate them up into $date and $time.
After some research through the php manual...I found that date(), date_format() and strtotime() are able to help me to separate them...(not sure if I am right)
But I am not very sure of how to code it out...
In my php file...the timestamp extracted would be $row['DATETIMEAPP'].
Will
$date = strtotime('d-m-Y',$row['DATETIMEAPP']);
$time = strtotime('Gi.s',$row['DATETIMEAPP']);
or
$date = date('d-m-Y',$row['DATETIMEAPP']);
work?
Can I use date() to get the time as well??
Thanks in advance
$timestamp = strtotime($row['DATETIMEAPP']);
gives you timestamp, which then you can use date to format:
$date = date('d-m-Y', $timestamp);
$time = date('Gi.s', $timestamp);
Alternatively
list($date, $time) = explode('|', date('d-m-Y|Gi.s', $timestamp));
If you dont want to change the format of date and time from the timestamp, you can use the explode function in php
$timestamp = "2012-04-02 02:57:54"
$datetime = explode(" ",$timestamp);
$date = $datetime[0];
$time = $datetime[1];
$mydatetime = "2012-04-02 02:57:54";
$datetimearray = explode(" ", $mydatetime);
$date = $datetimearray[0];
$time = $datetimearray[1];
$reformatted_date = date('d-m-Y',strtotime($date));
$reformatted_time = date('Gi.s',strtotime($time));
You can try this:
For Date:
$date = new DateTime($from_date);
$date = $date->format('d-m-Y');
For Time:
$time = new DateTime($from_date);
$time = $time->format('H:i:s');
$timestamp='2014-11-21 16:38:00';
list($date,$time)=explode(' ',$timestamp);
// just time
preg_match("/ (\d\d:\d\d):\d\d$/",$timestamp,$match);
echo "\n<br>".$match[1];
Works for me:
select DATE( FROM_UNIXTIME( columnname ) ) from tablename;
If you want to use the DateTime class, you can do so like this:
$timestamp = $row['DATETIMEAPP']; // String formatted as "2012-04-02 02:57:54"
// Create DateTime object from custom timestamp
$dt = DateTime::createFromFormat('Y-m-d H:i:s', $timestamp);
$date = $dt->format('d-m-Y'); // String variable of just the date
$time = $dt->format('H:i:s'); // String variable of just the time
And if you're concerned about using DateTime over strtotime() or date(), I'd like to point you in the direction of this conversation on StackOverflow titled "DateTime class vs. native PHP date-functions."
Optionally you can use database function for date/time formatting. For example in MySQL query use:
SELECT DATE_FORMAT(DATETIMEAPP,'%d-%m-%Y') AS date, DATE_FORMT(DATETIMEAPP,'%H:%i:%s') AS time FROM yourtable
I think that over databases provides solutions for date formatting too

Categories