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.
Related
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
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.
I have a collection of time records in a database in the format '09:51:06' (hour, minute, second).
I have a query which retrieves a bunch of these times from the database, but I only care for the Hour reference.
How can I get rid of the rest of the string and ad just the hour into an array? So in the example above, I just want to keep '09'.
I've looked into exploding and substrings, but can't seem to figure it out.
Thanks.
Exploding the string would look like this (you probably want to add intval() to be able to use it as a real number):
$hours = array_shift(explode(':', '09:51:06'));
But you are probably looking for the right query instead of doing this afterwards. If you are dealing with a time-type, you can use MySQL's date and time functions (HOUR() in this case):
SELECT HOUR(`time_column`) AS `hour` FROM `your_table`
Or from the database itself
SELECT TIME_FORMAT(field_name, '%H') as return_hour from table_name
$time_string='09:51:06';
$your_array[$array_index]=substr($time_string, 0, 2);
for more info on substr
I guess you can do this either thru string manipulation or time/date. Any worthwhile date way would be on the SQL side, PHP has mktime and date functions but you would have get the hour you're looking for intermediately along the way if you were to construct a date anyway
String
$thisHour = array_shift(explode(':', $timeString)); //this gets hour
$thisHour - substr($timeString, 0, 2); // or this
$hours[] = $thisHour; //this adds to array
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>";
Is there a way to change a date from
1985-12-15
to
1985-12
without using a regular Expression?
<?php echo date('Y-m', strtotime('1985-12-15')); ?>
That should do it.
This will, using strtotime, convert 1985-12-15 to a unix timestamp. The date function then takes a second parameter timestamp on which to format the date.
Convert the date to time via strtotime then use date to output in correct date format, like so:
<?php
echo date('Y-m', strtotime('1985-12-15'));
Maybe I'm just stupid, but if you only want the beginning of that date, stored as a string, can't you just use substr to extract the 7 characters at the beginning of that string ?
A bit like this, for instance :
$input = '1985-12-15';
$output = substr($input, 0, 7);
var_dump($output);
Which does give you :
string '1985-12' (length=7)
No need for any date-manipulation related function, in this case -- and this will probably be even faster/cheapier that parsing the string to a date and all that.
(Yeah, I know, premature optimisation ^^ )
$myDate = date('Y-m',strtotime('1985-12-15'));
echo $myDate // prints '1985-12'
If you think a regular expression isn't "cheap", then time functions will almost certainly be even more expensive: you would need to convert the string into a time value, then format it back into a string...
do you have that time value already in a array or do you whant such a like output come from the date function?
If you are not sure how the date will be formated, you should use the strtotime function, otherwise its probably as easy to do if the format is yyyy-mmm-dd or yy-m-d.
$datearray = explode('-',$date);
echo $datearray[0].'-'.$datearray[1];
not best to substr($date,0,strrpos($date,'-'));?
that would be cheapest?