I try to get the current timestamp from my mysql Server. I know the Command SELECT CURRENT_TIMESTAMP. But i need to echo it as a String. Does someone know how I can do this?
<?php
include_once("../DatenbankVerbindungen/dbconnect.php");
$query = mysqli_query($connection, "SELECT CURRENT_TIMESTAMP;");
echo gmdate("Y-m-d\TH:i:s\Z", $query);
mysqli_close($connection);
?>
Alias the current timestamp and then access it in your PHP code:
$query = "SELECT CURRENT_TIMESTAMP AS ct;";
$rlt = mysqli_query($connection, $query);
$r = mysqli_fetch_assoc($rlt);
echo gmdate("Y-m-d\TH:i:s\Z", $r['ct']);
Or, as #Gordon suggested, you do the formatting in MySQL directly and just return a formatted timestamp string:
$query = "SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') AS ct;";
$rlt = mysqli_query($connection, $query);
$r = mysqli_fetch_assoc($rlt);
echo $r['ct'];
Related
I was able to apply this line onto phpMyAdmin and it worked just fine.
SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY date DESC LIMIT 1
The problem is that when I added the rest of the code, the recent date shows up blank on the webpage. Am I missing something in this code?
<?php
$query = "SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY date DESC LIMIT 1";
$result = mysql_query($query);
echo "$date";
?>
Any help is appreciated. Thank you.
. Try this
$query = "SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY. date DESC LIMIT 1";
$result = mysql_query($query);
$r = mysql_fetch_assoc($result);
$date = $r['date'];
echo "$date";
You didn't set $date variable. You need to use mysql_fetch_array function for your $result variable.
Ex:
`
$query = "SELECT id, date_format('date', '%m.%d.%Y') as 'date' FROM TABLE ORDER BY date DESC LIMIT 1";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print_r($row); }
`
hello im having some problem with this sum select does anyone know whats wrong with this, it seems that im getting no result from it
$result = mysql_query("SELECT SUM(total)
FROM table1 where date BETWEEN '".$date1." 00:00:00' AND '".$date2." 23:59:59' and username = ".$user."");
while($row=mysql_fetch_array($result))
{ $sum = $row['SUM(total)'];}
Try
$query = "SELECT SUM(total) AS result
FROM table1
WHERE DATE(date) BETWEEN '$date1' AND '$date2'
AND username = '$user'";
$con = \\your db connection string
$result = mysql_query($con,$query);
while($row=mysql_fetch_array($result))
{$sum = $row['result'];}
In my mysql database dateTime is stored in the following form.
00000-00-00 00:00:00
In my php I want to convert it to tiemStamp form like this.
136716425
I tried using
$date2->getTimestamp();
without success, what function should I use to change the format to timestamp?
$sql = "SELECT * FROM temp_user";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$date1 = new DateTime();
$date2 = $row[dateTime];
echo $date1->getTimestamp();
echo $date2->getTimestamp();
}
MySQL has a builtin function for that called UNIX_TIMESTAMP
SELECT UNIX_TIMESTAMP(NOW())
SQLFiddle Demo
UNIX_TIMESTAMP
UPDATE
$sql = "SELECT *,UNIX_TIMESTAMP(dateTime) unx FROM temp_user";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
$var = $row['unx'];
// other codes
}
I have this table :
I would like to delete same rows. For example first five rows are the same, my table should have only one row that includes this data : 40.792274 29.412994 2011-12-21 17:19:52.
So I used the following code :
$query = "SELECT * FROM table GROUP BY date";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$date = $row['date'];
$lat = $row['latitude'];
$lon = $row['longitude'];
$query = "SELECT * FROM table WHERE date='$date' AND latitude='$lat' AND longitude='$lon'";
$re = mysql_query($query);
$number = mysql_num_rows($re);
$number--;
$query = "DELETE * FROM table WHERE date='$date' AND latitude='$lat' AND longitude='$lon' LIMIT $number";
mysql_query($query);
}
But this code doesn't work.. What should I do ?
Edited :
I solved my question :
$query = "SELECT * FROM table GROUP BY date";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$date = $row['date'];
$lat = $row['latitude'];
$lon = $row['longitude'];
$query = "SELECT * FROM table WHERE date='$date' AND latitude=$lat AND longitude=$lon";
$re = mysql_query($query);
$number = mysql_num_rows($re);
$number--;
$query = "DELETE FROM table WHERE date='$date' AND latitude=$lat AND longitude=$lon LIMIT $number";
mysql_query($query);
}
Query lines were incorrect in my first question.
To remove the duplicate elements, you would use something like this:
$q = "SELECT date FROM table GROUP BY date"
$r = mysql_query($r);
$date = '';
while($row = mysql_fetch_array($r)){
$date = $row['date'];
$q = "SELECT date FROM mytable WHERE date='$date'";
$re = mysql_query($q);
$num = mysql_num_rows($re);
$num--;
$q = "DROP FROM mytable WHERE date='$date' LIMIT $num";
mysql_query($q);
}
Should do the trick. More specifically, when creating your $date value, you have to provide PHP with a time to use. date() defaults to using the current time, but you can provide it with a custom time as the second argument.
I suggest you take a look at the strtotime() manual at php.net as well (To translate times in your db to timestamps that can be used with date() ).
EDIT: The Answer above has been edited to remove all duplicate entries.
Try changing $dateOfNewData = date('Y-m-d H:i:s');
to
$dateOfNewData = date('Y-m-d 00:00:00'); //or change the first 00 to H if you need it to match by hour, second 00 to i if you need to match minutes and the same with seconds.
or $dateOfNewData = date('Y-m-d') which is pretty much the same and works with datetime field types
And you also need to modify your query to something like this unless you need an exact time:
"SELECT * FROM mytable WHERE date = '$dateOfNewData'" // you might also want the end date if you're working with the past in your database.
Well you can try like "Ignas" suggest but you cal also try this:
First just get the date (year, month, day) without hour, minutes and seconds. If you use full date format then you need to match exactly the same time. (to second the same) which is not really what you are looking for i guess. So you can use this:
$dateOfNewData = date('Y-m-d'); //just get year, month, day in right format (2011-12-20)
Then run a query. Here you have more options but i think the easier is something like that:
"SELECT * FROM mytable WHERE date_col LIKE '$dateOfNewData%' GROUP BY date_col"
This will group the same dates together and will display just once and will match all the rows where 'date_col starts with example: 2011-12-20% (thats why i use LIKE and $dateOfNewData%)
$dateOfNewData contains current date in this format:year-month-day (2011-12-20) and in Mysql query dont forget to use % at the end of the date. It's like * in windows for example.
'mytable' replace with your table name and 'date_col' with date column.
date() you have used will give current date time , so try to use mktime() to get extact date time you want.
you have to change your query little bit, I have modified query below,
$query = mysql_query("SELECT * FROM mytable WHERE date='$dateOfNewData'");
In mysql Date or datetime coulmn should be within ''.
I'm using the following code to sort MySQL queries into time/date:
mysql_select_db("user_live_now", $con);
$result = mysql_query("SELECT * FROM users_newest_post ORDER BY users_date_post DESC");
while($row = mysql_fetch_array($result))
{
print($row['user']);
}
instead of having the PHP run through and show all the values in the table can I have it show the values from an array?
So, you want to find specific users in the SQL query to return? Build your query programmatically:
$users = array('User1','John','Pete Allport','etc');
$sql = "SELECT * FROM `users_newest_post` WHERE ";
$i = 1;
foreach($users as $user)
{
$sql .= "`username` = '$user'";
if($i != count($users))
{
$sql .= " OR ";
}
$i++;
}
$sql .= " ORDER BY `users_date_post` DESC";
$result = mysql_query($sql);
Which would get you a query like:
SELECT * FROM `users_newest_post`
WHERE `username` = 'User1'
OR `username` = 'John'
OR `username` = 'Pete Allport'
OR `username` = 'etc'
ORDER BY `users_date_post`
DESC
So, you want to find all posts for a certain date or between two dates, kinda hard to do it without knowing the table structure, but you'd do it with something like this:
//Here's how to find all posts for a single date for all users
$date = date('Y-m-d',$timestamp);
//You'd pull the timestamp/date in from a form on another page or where ever
//Like a calendar with links on the days which have posts and pass the day
//selected through $_GET like page.php?date=1302115769
//timestamps are in UNIX timestamp format, such as you'd get from time() or strtotime()
//Note that, without a timestamp parameter passed to date() it uses the current time() instead
$sql = "SELECT * FROM `posts` WHERE `users_date_post` = '$date'"
$results = mysql_query($sql);
while($row = mysql_fetch_assoc($results))
{
echo $row['post_name'] . $row['users_date_post']; //output something from the posts
}
//Here's how to find all posts for a range of dates
$startdate = date('Y-m-d',$starttimestamp);
$enddate = date('Y-m-d',$endtimestamp);
//Yet again, date ranges need to be pulled in from somewhere, like $_GET or a POSTed form.
//Can also just pull in a formatted date rather than a timestamp and use it straight up instead, rather than going through date()
$sql = "SELECT * FROM `posts` WHERE `users_date_post` BETWEEN '$startdate' AND '$enddate'";
//could also do:
//"SELECT * FROM `posts` WHERE `users_date_post` > '$startdate' AND `users_date_post` < '$endate'"
$results = mysql_query($sql);
while($row = mysql_fetch_assoc($results))
{
//output data
}
To find posts for a specific user you would modify the statement to be something like:
$userid = 5; //Pulled in from form or $_GET or whatever
"SELECT * FROM `posts` WHERE `users_date_post` > '$startdate' AND `users_date_post` < '$enddate' AND `userid` = $userid"
To dump the result into an array do the following:
mysql_select_db("user_live_now", $con);
$result = mysql_query("SELECT * FROM users_newest_post ORDER BY users_date_post DESC");
while($row=mysql_fetch_assoc($result))
{
$newarray[]=$row
}
What you probably want to do is this:
$users = array("Pete", "Jon", "Steffi");
$users = array_map("mysql_real_escape_string", $users);
$users = implode(",", $users);
..("SELECT * FROM users_newest_post WHERE FIND_IN_SET(user, '$users')");
The FIND_IN_SET function is a but inefficient for this purpose. But you could transition to an IN clause with a bit more typing if there's a real need.
$sql = 'SELECT * FROM `users_newest_post` WHERE username IN (' . implode(',', $users) . ')';