Split datetime variable in PHP into date and time - php

How can I reformat/split the datetime data (from a database) into two variables, date and time, and then add them back to the array?
At present $datetime contains data such as: 2014-12-03 00:00:00
I want to split it out into $date and $time
I have the following, but when I echo out the $date and $time vars they are empty:
$datetime = date($data['date_time']);
$date = date_format($datetime,"Y-m-d'");
$time = date_format($datetime,"H:i");

DateTime objects are created using date_create(), not date()
$datetime = date_create($data['date_time']);
$date = date_format($datetime,"Y-m-d");
$time = date_format($datetime,"H:i");

Why not simply do it using DateTime, in OOP way
$date = new DateTime('2000-12-31 00:00:00');
$dateonly=$date->format('Y-m-d');
$timeonly=$date->format('H:i');
DEMO

With your current code you can try this :
$datetime = date($data['date_time']);
$dateArray = explode($datetime, " ");
$date = $dateArray[0];
$time = $dateArray[1];

I would do:
$datetime = new DateTime($data['date_time']);
$date = $datetime->format('Y-m-d');
$time = $datetime->format('H:i:s');

You can use strtotime:
$datetime = date($data['date_time']);
$date = date("Y-m-d",strtotime($datetime));
$time = date("H:i:s",strtotime($datetime));

Related

how can i subtract an hour from datetime string eg: "2017-04-20 11:46:00" and convert it back to string in PHP?

i need to subtract an hour from a string '2017-04-20 11:46:00' and convert it back to the same format.
I have tried below function
<?php
$date = new DateTime('2017-04-20 11:46:00');
$tosub = new DateInterval('PT01H00M');
$result =date_format($tosub,"Y-m-d H:i:s");
echo $result;
?>
You need to apply the subtract function first, and then convert to the desired format.
$date = new DateTime('2017-04-20 11:46:00');
$tosub = new DateInterval('PT01H00M');
$result = $date->sub($tosub); // Add subtract function
echo $result->format("Y-m-d H:i:s");
$date = date('Y-m-d H:i:s');
$time = strtotime($date);
$time = $time - (60*60); //one hour
echo $new_date = date("Y-m-d H:i:s", $time);
This is a one-liner that will reduce your sample datetime by 1 hour:
Code:
$one_hour_ago=(new DateTime('2017-04-20 11:46:00'))->modify('-1 hour')->format("Y-m-d H:i:s");
echo $one_hour_ago;
Output:
2017-04-20 10:46:00

PHP get timestamp from separate date and time

I have two inputs, one for the date in yyyy/mm/dd format and another for time in 12:15 AM. Now I need to save into the the databse a timestamp. I get both inputs lets say:
$my_date = '2013/12/22';
$my_time = '12:50 PM';
How do I get a timestamp to save into db?
Thank you
Give this a try...
$my_date = '2013/12/22';
$my_time = '12:50 PM';
$full_date = $my_date . ' ' . $my_time;
$timestamp = strtotime($full_date);
Use DateTime:createFromFormat()
<?php
$my_date = '2013/12/22';
$my_time = '12:50 PM';
$d = sprintf('%s %s', $my_date, $my_time);
$dt = DateTime::createFromFormat('Y/m/d h:i A', $d);
$ts = $dt->getTimestamp();
var_dump($ts);
Yields:
int 1387716600
Hope this helps :)
Edit
PHP's date format reference
You could use strtotime() to convert it into an UNIX timestamp.
E.g.:
$my_date = strtotime('2013/12/22');
$my_time = strtotime('12:50 PM');
Full date + time timestamp
$timestamp = strtotime('2013/12/22 12:50 PM');
More info: http://php.net/manual/en/function.strtotime.php

Get date, hour and minute as separate variables from a datetimestamp in PHP

In PHP if I have a variable ($getTimeStamp) that follows the format 0000-00-00 00:00:00 (i.e. 2013-09-26 13:06:00).
What is the easiest way to get the date ($getDate), hour ($getHour) and minute ($getMinute) as separate variables?
The easiest way is to use PHP DateTime class
$getTimeStamp = '2013-09-26 13:06:00';
$date = new \DateTime($getTimeStamp);
$dateString = $date->format('Y-m-d');
$hourString = $date->format('H');
$minuteString = $date->format('i');
It's not timestamp ;) Check what time() function returns, that's how timestamp looks like.
You can use something like that:
$time = strtotime($getTimeStamp);
$getDate = date('Y-m-d', $time);
$getHour = date('H', $time);
$getMinute = date('i', $time);
You can simply achieve this using -
$time = strtotime($getTimeStamp);
$getDate = date('Y-m-d', $time);
$getHour = date('H', $time);
$getMinute = date('i', $time);
Have a look at time() and date().
Well, since all you want is really to parse some text this would be the shortest way:
list($date, $hour, $minute) = preg_split('/[ :]/', $getTimeStamp);
There is no real reason to involve the date/time classes if you are not manipulating dates or calculating timestamps.
Live example.
Just one answer using the DateTime class
$dt = DateTime::createFromFormat('Y-m-d H:i:s', $getTimeStamp);
$date = $dt->format('Y-m-d ');
$hour = $dt->format('H');
$minutes = $dt->format('i');

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

How to separate date and time using php from date time string?

How to convert this string
$parent = "2011-08-04 15:00:01";
to separate it into two strings:
$child1= "2011-08-04";
$child2 = "12:00:01";
And then convert
$child1 to this format 8.4.2011
and
$child2 to this format 15:00
$time = new DateTime("2011-08-04 15:00:01");
$date = $time->format('n.j.Y');
$time = $time->format('H:i');
$parent = '2011-08-04 15:00:01';
$timestamp = strtotime($parent);
$child1 = date('n.j.Y', $timestamp); // d.m.YYYY
$child2 = date('H:i', $timestamp); // HH:ss
date('Y-m-d', strtotime( '2015-04-16 15:00:01' ) );
this will give you correct date format followed by mysql
i.e 2015-04-16 03:54:17

Categories