Date and Time (2011-04-19 00:00:00).... how do I add time?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Date and Time (2011-04-19 00:00:00).... how do I add time?

Post by simonmlewis »

Code: Select all

2011-04-19 00:00:00
I have set the Date Time field in my database.
I know how to add the date:
$today = (date('Y-m-d'));

But how do I use the same sort of simple format, to add the date and the time?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by fugix »

use the time() function using the format that you want
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by simonmlewis »

Why does this happen:

$today = (date('Y-m-d'));
$time = (time('hh-mm-ss'));
echo "$time, $today";
Produces:1303215495, 2011-04-19
Why does it not product 13:18:24, 2011-04-19 ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by simonmlewis »

bingo:
$time = date('Y-m-d H:i:s');
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by fugix »

yes you could do that too. glad you figured it out
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by simonmlewis »

Ok - so I've found out how to add the Date and Time into the 'Date Time' field.

But now it's onto the reporting and I want to query by Date only - and then show all items within that date.

How do I query the date part of '2011-04-19 17:15:30'?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by fugix »

what are you going to be comparing the date in your database to?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by simonmlewis »

Nothing.
Just that if there are thousands of entries - I can query it by date. Do like a distinct query first by date, then query all those by that date.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by simonmlewis »

<?php
$string = "2011-04-20 13:15:45";
$a = substr($string, 0,10);
echo "$a";
?>

Think this is the answer.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by simonmlewis »

Code: Select all

$result = mysql_query ("SELECT DISTINCT dateviewed FROM recent") or die(mysql_error());
echo "<form method='post' action='index.php?page=test'>
<select name='datesub'>";
  while ($row = mysql_fetch_object($result))
  {
  $date = substr($row->dateviewed, 0,10);
  echo "<option value='$date'>$date</option>";
  } mysql_free_result($result);
  
  echo "</select><input type='submit' value='submit'></form>";
Is it possible to extract the first 10 characters from within the MySQL query? As this just produces all the dates there are in there.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by McInfo »

MySQL Date and Time Functions

Code: Select all

SELECT DATE('2011-04-20 12:00:00') # Returns '2011-04-20'
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by simonmlewis »

Thanks for that massive page to search thru - not sure what exactly you are directing me to on the page.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by simonmlewis »

Ok - forget that query, don't need it. moving on, we now have a problem where we are getting so many entries it is slowing down the web site.

The new idea is that as people visit the items, the system will check if they have visited it before. If they have, it doesn't enter it into a row in the db table - that's easy.
But, we want to only keep the LAST FIVE entries they made.

How do I check for the LAST FIVE, keep them, but delete any before. My suspicion is that you somehow spot the oldest ID and delete that, so if you have IDs..

76893
77590
80444
81990
82818

And then they go for another product which enters
82998

Then somehow, it knows to delete row:
76893

How do I find that row??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by pickle »

Sort by ID and limit the query to only return 5 rows.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Date and Time (2011-04-19 00:00:00).... how do I add tim

Post by simonmlewis »

But how do I then remove "the others"? To keep it to 5 rows?

As I said - if they have 5 rows in there already, and then visit another item, that adds a row. How do I delete the oldest row? (the lowest ID)
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply