Convert time() from SQL for monthly sales PHP - php

I want to make monthly sales from the sales table but i store date data using time() format
Example :
UserID | Product | Date
1 | Hot Dog | 1504363230
4 | Chicken | 1504695631
1 | Potato | 1504761716
3 | Spinach | 1505003789
So, how can i create monthly sales from there without replace Date into date() ? Because it will take a long time if i have to change 300K row
What should i do ?
Select * FROM Sales WHERE UserID = UserID AND Date = ?
the output must be like this
UserID 1 do 2 transaction in the last month or UserID 1 do 3 transaction in this
month

A PHP solution would be to use the second parameter in the date function:
string date ( string $format [, int $timestamp = time() ] )
The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().

You can convert a unix timestamp to a datetime string in MySQL with FROM_UNIXTIME.
SELECT FROM_UNIXTIME(`Date`) as `Datetime` FROM sales
You can then expand on this with other date functions like MONTH or YEAR
SELECT MONTH(FROM_UNIXTIME(`Date`)) as `Month` FROM sales

EDIT:
I think this should work for you. I guess you want to avoid PHP's date() but MySQL's DATE() is okay.
SELECT
COUNT(*) AS nb_transaction,
UserID
FROM
sales
WHERE
DATE(`Date`) LIKE '2018-06-%'
Of course replace the month with the one you are looking for.
SQL (will be faster than PHP):
You can convert any UNIX Timestamp using FROM_UNIXTIME() to any format:
FROM_UNIXTIME(`Date`, '%Y %D %M %h:%i:%s')
PHP:
The time() function returns a UNIX Timestamp.
If you want to display a date using one, you can use DateTime::setTimestamp:
$date = new DateTime();
$date->setTimestamp($row['Date']);
echo $date->format('Y-m-d H:i:s'); // Hot Dog = 2017-09-02 14:40:30
Or shortly:
echo (new DateTime())->setTimestamp($row['Date'])->format('Y-m-d H:i:s');

$timestamp = 1517317337107; $date=date('Y-m-d H:i:s',
$timestamp/1000);
echo "date and time : ". $date."<br>";
$month=date('Y-m-d',$timestamp/1000);
echo "only date : ".$month."<br>";
#output
date and time : 2018-01-30 13:02:17
only date : 2018-01-30
hope this will help

Related

Mysql SELECT by date format as 24 hour

I am trying to select data from a table mysql but I need to format a varchar date column in a military format, but cannot get my desire output my query is below.
"SELECT STR_TO_DATE(`hour`, '%Y-%m-%d %k:%i') AS `time` FROM `dataTable`"
When I tried to echo the time below format appear.
2017-09-21 00:00:00
My desire output is below which is in 24 hour format and removing the seconds, any suggestions would be great.
2017-09-21 18:00
Use the Date_format function of mysql.
"SELECT DATE_FORMAT(`hour`, '%Y-%m-%d %H:%i') AS `time` FROM `dataTable`"
Explanation:
The STR_TO_DATE function always return the date in the format of 2008-09-15 22:23:00
SELECT STR_TO_DATE('Monday 15th September 2008 22:23:00', '%W %D %M %Y %H:%i:%s');
+----------------------------------------------------------------------------+
| STR_TO_DATE('Monday 15th September 2008 22:23:00', '%W %D %M %Y %H:%i:%s') |
+----------------------------------------------------------------------------+
| 2008-09-15 22:23:00 |
+----------------------------------------------------------------------------+
1 row in set (0.00 sec)
Check the above example: the second parameter in STR_TO_DATE function is the format of the parameter 1 to tell the function that in which format parameter 1 is passed in the function.
Reference taken from this link.

PHP & MySQL query date time stamp between

I am attempting to query for a record. The date is stored as a date time stamp and my query looks like this:
SELECT count(c.id) as totalOrders
FROM Cart c
WHERE c.artist_id = 1
AND c.paid = 1
AND date_format(c.created, 'Y-m-d') between '2016-09-06' AND '2016-09-07'
My date time stamp is this: 2016-09-07 21:04:46
For some reason this does not return any records, why?
Change the following line:
AND date_format(c.created, 'Y-m-d') between '2016-09-06' AND '2016-09-07'
to
AND date(created) between '2016-09-06' AND '2016-09-07'
date() function will return the date from datetime and it will check in the given range.
use date() function instead of date_format()
SELECT count(c.id) as totalOrders
FROM Cart c
WHERE c.artist_id = 1
AND c.paid = 1
AND date(c.created) between '2016-09-06' AND '2016-09-07'

MySQL query select all were date is equal to today on datetime

I have a database table something like this:
Col_1 | Datetime
test1 2015-12-19 09:00:00
test2 2015-12-18 12:30:00
test3 2015-12-19 10:00:00
test4 2015-12-19 16:45:00
I am trying to select in my query all results where the datetime is equal to today's date, although when I simply use something like:
$today = date("Y-m-d");
SELECT * FROM table WHERE datetime = '$today'
It just doesn't seem to work? And if I add the time in to $today it would only select those results which are equal to the exact current time which is not what I want?
Any idea as to how I can do this? I've even gone so far as to this which is totally stupid (Although it was about 3am in the morning when I coded it):
$now = date("Y-m-d H:m:s", strtotime('midnight'));
$todays = date(strtotime('midnight'));
$take24hours = date("Y-m-d H:m:s", strtotime('-24 hours', $todays));
$results = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM table WHERE date_added > '$take24hours' AND date_added < '$now')"));
Can do this entirely in sql transaction ( no need for php date formatting ) :
SELECT * FROM table WHERE date(`datetime`) = current_date;
CURDATE() will get current date.
SELECT * FROM table where DATE(date)=CURDATE()

Comparing current date/time with date/time in a database

I need to create something to display an image based on the current date and time. I have an SQL table set up that looks like this:
eventid | startdate | enddate | imgfile | status
6 | 2013-04-29 | 2013-05-03 | finals.jpg | active
What I need to do is compare the start date and end date to the current date, and if the current date falls within the startdate and enddate, then display the imgfile.
I'm not sure how to go about this, but this is what I tried.
//get current date and time
$currentdatetime = strtotime(now);
$formatteddatetime = date("y-m-d", $currentdatetime);
while($row=mysql_fetch_array($getimage))
{
//get variable for startdate
$formattedstartdate = date("y-m-d", $row[startdate]);
$formattedenddate = date("y-m-d", $row[enddate]);
if($formatteddatetime >= $formattedstartdate AND $formatteddatetime <= $formattedenddate AND $row[status] == 'active')
{
echo $row[imgfile];
}
}
?>
Right now, it doesn't show anything inside of the entire while statement. Am I at least on the right track?
the CURRENT_DATE returns the current date in ‘YYYY-MM-DD’ format or YYYYMMDD format depending on whether numeric or string is used in the function. CURDATE() and CURRENT_DATE() are the synonym of CURRENT_DATE.
You should do it in your SQL query instead of PHP. For example:
SELECT * FROM table WHERE CURRENT_DATE BETWEEN startdate AND enddate;
CURRENT_DATE() is a MySQL function that returns the current date, as the name suggests. It is one of a few special functions that have an alias without the parentheses.

from date to date - search and display

I have a database holding two bits of information. from date to date. I need this entry to appear in my calender on every day, but I can only manage to get to appear on the first and last date.
example of DB:
job_no | date1(from)| date2(to)
________________________________
1 |2013-01-28 | 2013-02-03
2 |2013-01-14 | 2013-01-18
Edit for question. the search bar I have allows for ONE date input and the the calender finds entries through date1 and the next 6 days.
I cannot have a search which contains two date inputs because my users are so used to this way and i do not want to increase searching time. I started to think that I had to find the dates between the dates, add that to an array then use an if statement to find matches...but even saying this makes no sense to me.
regarding job 1, I need my calender to show this job up on all dates 28/29/30/31/01/02/03.
My current search SELECT * FROM jobs WHERE date1='$searchinput' PER day and calender this search. I use strtotime to increase the input date by +1 to add to the search for each day.
Calender page.
What I want with my results. User searched Date 28th.
Mon 28 | Tues 29 | Wed 30 ...... | Fri 03 |
_________________________________________________________________________
Job no 1 | job no 1 | job no 1 ...... | job no 1
What I have now.
Mon 28 | Tues 29 | Wed 30 ...... | Fri 03 |
_________________________________________________________________________
Job no 1 | blank | blank | job no 1
each day has a new select query right now. It matches days with date 1 and date 2. I dont need this as before I only had jobs out on one day now they go out for more than one day and need the job to be noted on all days it is out by only using a job from date and job to date.
EDIT 2:
SELECT * FROM calender_db NATURAL JOIN job_db
WHERE section='two'
AND date1 < '$day' AND date2 > '$day'
OR date1 = '$day' OR date2 = '$day'
This query selects what I need, but as I am using OR the first WHERE CLAUSE can be null. I need that to always be in the clause. I have been looking at using IIF or CASE to rectify but do not how to implement 100%...?
why not use BETWEEN
SELECT * FROM tableName WHERE date BETWEEN 'date1' AND 'date2'
SQLFiddle Demo
UPDATE 1
SELECT *
FROM tableName
WHERE date BETWEEN '2013-01-28' AND '2013-01-28' + INTERVAL 6 DAY
SQLFiddle Demo
To generate a list of days between two dates:
$days = array();
$stop = strtotime($date2);
for ($current = strtotime($date1); $current <= $stop; $current = strtotime('+1 days', $current)) {
$days[] = date('d', $current);
}
echo join('/', $days);
Demo
Update
Misunderstood the question it seems, if both dates are stored as columns and you're querying with a single date:
SELECT *
FROM jobs
WHERE 'date_from_input' BETWEEN date1 AND date2
Update 2
Your latest query can be written as:
SELECT *
FROM calender_db NATURAL JOIN job_db
WHERE section='two' AND '$day' BETWEEN date1 AND date2
Try like this
SELECT * FROM Mytimetable WHERE date BETWEEN 'date1' AND 'date2'
Query between date1 - date2
SELECT *
FROM `objects`
WHERE (date_field BETWEEN '2013-01-30 14:15:55' AND '2013-02-19 10:15:55')
OR
SELECT *
FROM `objects`
WHERE (date_field BETWEEN date1 AND date2)
query to select dates between two dates with PHP variables.
i will explain with exapmle :
$date1 = "2013-02-19";
$date2 = "2013-02-25";
then sql query will be :
$query = " SELECT * FROM table_xyz where (date_item BETWEEN date('".$date1."') AND date('".$date2."')";
hope it solves your problem
SELECT
*
FROM
tablename
WHERE
date between '$date1' and '$date2'
order by
date

Categories