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
Related
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));
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');
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();
I have a timestamp in the database. With the following code I can format it to the right date:
$datefrom=mysql_real_escape_string($record['projectdatefrom']);
$date1 = date("Y/m/d", $datefrom);
Then I give the input vield the value="$date1.
Now I have another field for the H:i, so I'd like to have them seperate from each other.
Can can I cut the Y/m/d of the $date1 and only return the H:i?
Was trying doing things like this: $datetest = date("H:i", $datefrom); but no success.
$datefrom will need to be a UNIX timestamp. strtotime() can be useful for generating one off a plain-text or MySQL-style date string.
There is no need to escape the string after it was returned from the DB.
Therefore:
$date = date("Y/m/d", strtotime($record['projectdatefrom']));
$time = date("H:i", strtotime($record['projectdatefrom']));
Or using DateTime:
$dt = new DateTime($record['projectdatefrom']);
$date = $dt->format('Y/m/d');
$time = $dt->format('H:i');
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