How to split date and time value to a time - php

2016年03月12日 9時00分
This is returned by $this->input->post(date_time).I want to show only 0900. How to get this output by above one?

There is some unclear charectors in your date string, If the date string is in correct format you can use the below code to get only time part.
$dd = "2016-03-12 9:00";
echo date("Hi",strtotime($dd));
May be this will help you

Related

how can i convert time '08:60:02' to '09:00:02' using php?

I have a special requirement where need to sum time columns, but for few record sum time is coming '08:60:02' so i need to get it as '09:00:02'. I tried to convert it to seconds first and then convert back to h:m:s but it's giving null as result.
$a = date_create_from_format("H:i:s", "08:60:02");
echo $a->format("H:i:s");
shows
09:00:02
you can try mysql functions like
SELECT round((SUM((08:60:02))/60)/100, 1);

Timestamp Difference and Output

In My SQL Database I have a Timestamp Column with values like this one representing the Date of the last edit:
2015-01-17 08:55:34.000000
I want to compare the Date with the current date and when is the same day I want to echo Today and otherwise I want to Display the Date of the last edit:
$timefromdb = '2015-01-17 08:55:34.000000'
$edit = strtotime($timefromdb);
if($edit > $_SERVER['REQUEST_TIME']){echo "Today";}
else{
echo strftime("on %A, the %d %B %Y", $edit);
}
echo " at ".date('h:i',$edit)
It always Displays 01/01/1970. There must be a Problem with strtotime. I did a bit of research and it seems like my Timestamp Format isn't a valid one: http://php.net/manual/en/datetime.formats.php
Around the web are a lot of Questions about converting Timestamps but I just can't find the right one: I also got a bit confused by all the functions to convert date stuff.
So can someone Tell me how to get a valid Timestamp for using it in strftime and to compare it to the REQUEST_TIME.
Thanks in Advance!
UPDATE: As Always: The Problem sits in Front of the PC. I declared the Variable but never assgined the Timestamp to it :)
Chop off the .000000 from the date as it makes the date a format strtotime() cannot work with. There's several ways to do this. A simple substr is one of them.
$timefromdb = substr('2015-01-17 08:55:34.000000', 0, -7);
I'm not exactly understood you, but
try
1. compare gettype( $edit ) and gettype($_SERVER['REQUEST_TIME'])
2. not sure what $timefromdb will be more then $_SERVER['REQUEST_TIME'], because IMHO when user edited data, time of it action will me less then current time.

PHP date function does not print correct format

Can any one please help me to find out that what is wrong with following date() function format ??
$advised_time=date("D, d/n/Y", strtotime($this->input->post('advised_sign_on_date')));
The post variable $this->input->post('advised_sign_on_date') contains the date like : "11-12-2014"
when I print it shows the date format something like Thu, 11V12V2014. However, the format is fine but I do not understand why is V coming in instead of /.
Update
I figured out that this is happening because of json_encode. I was printing the json_encode array when I print the $advised_time it shows me correct format but I json_encode it escapes the slashes i guess. How can I avoid to do so ?
use default_timezone_set to set default timezone
date_default_timezone_set('your time zone');
find your time zone on:
click here

PHP format date from returned data

I have a query that is returning a grid. One of the columns brings back a column with a date, like this:
echo "<td>{$Row[ETA]}</td>";
This displays the ETA from the database like this:
2013-10-30 20:00:0
I basically want to remove the TIME portion and just keep the date. Can this be done in the TD or do I have to the conversion elsewhere? I would like to just do the conversion within the cell, if possible.
Let me know how this can be done.
You can use the strtotime() and date() functions to achieve this!!!
date("Y-m-d", strtotime($Row[ETA]));
Well! if you want to get just date then you should use this function in your query
DATE(date_field)
as this will return only date from the datetime column
Ideally you should listen to the suggestion by JohnConde because it will limit your overhead between the database and your script but you can also substr() on the fly if you wish like this:
echo "<td>".substr($Row['ETA'], 0, 10)."</td>";
You have to echo it differently:
$eta = $Row['ETA'];
$etaDate = date("Y-m-d", strtotime($eta));
//now use $etaDate
You can use the date function to format the time, as what you're getting is a date as a string, you can use strtotime.
I think the format you're looking for is: date("Y-m-d", strtotime($Row["ETA"]));, you can either parse that into a variable and save it there, or you can concatenate the results together for the final string.

why am I getting '4:00' when I'm trying to display time using the date function with php?

I am trying to display a time I have in my database. I managed to have it display a time in the correct format for what I need, but for some reason, it is only displaying '4:00' every time.
Here is my code:
date('g:i', strtotime($row['startTime']))
An example of I have the time displayed in my database is like this: 00:12:30
Why is it showing '4:00' every time?
strtotime expects a datetime format ... you should do
date('g:i', strtotime('01 January 2009 ' . $row['startTime']))
Whats the underlying database, and what datatype does the startTime column have? Peering at the closest php code I have, strtoime works fine with a DATETIME representation in the DB (MySQL).
strtotime converts a date time string to a Unix timestamp.
Perhaps your $row['startTime'] doesn't qualify as a date time string.
None of the examples here discussed a date time string which did not include a date.
The link also said that if strtotime is confused, it returns random results. I would add a few more format characters and see what else is returned.
As noted the problem is the use of strtotime(). The following works on my machine, if it's of any use:
$date_text = $row['startTime']; // assuming the format "00:12:30"
list($hrs,$mins,$secs) = explode(":",$date_text); // in response to the question in the comments
/* the explode() takes the string "00:12:30" and breaks into three components "00","12" and "30".
these components are named, by their order in the array formed by explode(), as $hrs, $mins and $secs.
see: http://us3.php.net/manual/en/function.explode.php
and: http://us3.php.net/manual/en/function.list.php
*/
echo "<p>" . date("g:i",mktime($hrs,$mins,$secs)) . "</p>";

Categories