In PHP convert date in to time format output - php

In my database field there in date and time saved using time(); function.
I want to compare that one with given date in date format like '20/02/2015 '
my query is like:
select view,optin,insertTime,isUnique from sg_page_report as PR where insertTime='20/02/2015'"
where insertTime contains different format date like '1393587636'
How can I resolve this one?

WHERE FROM_UNIXTIME(insertTime)='2015-02-20'
Or you can do it like
$ts=mktime(0,0,0,2,20,2015); // or
//$ts=strtotime('20/02/2015'); // inefficient
And your query can be modified to look like
WHERE insertTime=$ts

Related

Date in varchar format not working with query with date

I have a field name 'orderdate' (type Date format yyyy-mm-dd).when i am executing the following query it doesnot show any result.What may be the problem, is the date format used in my query creating problem.plz soggest any solution .I can not change the format of orderdate in table. my query is
SELECT
stationerytype,
txntype,
stationeryqtyrecd,
stationeryqtyissued,
stationeryissueddate,
orderdate,
SUM(stationeryqtyrecd) as ttlreceived
FROM tblstationerystock
WHERE
(
stationerytype ='A4 White Ream' AND
txntype ='received'
) AND orderdate<07/09/21
Simply enclose your date in a Single ' quote or Double " qoute like orderdate<'2021-07-09'; and kindly try using the default MySQL Date Format yyyy-MM-dd.
SELECT stationerytype,
txntype,
stationeryqtyrecd,
stationeryqtyissued,
stationeryissueddate,
orderdate,SUM(stationeryqtyrecd) as ttlreceived
FROM tblstationerystock
WHERE (stationerytype ='A4 White Ream' AND txntype ='received') AND orderdate<'2021-07-09';
MySQL DATE is one of the five temporal data types used for managing date values. MySQL uses yyyy-mm-dd format for storing a date value. This format is fixed and it is not possible to change it.
For example, you may prefer to use mm-dd-yyyy format but you can’t. Instead, you follow the standard date format and use the DATE_FORMAT function to format the date the way you want.
Check this.

Access97 Compare two Dates

I'm trying to compare 2 dates with an SQL Query,
My query is this :
"SELECT table.toto
FROM table
WHERE
table.date < $date"
$date is a variable that comes from my PHP code
In my table, my date is formatted like this : dd/mm/YYYY
Unfortunately, it doesn't work as expected, it returns only one result (it's supposed to return way more results), which date is : 1312-09-15 00:00:00 but in database, the date is formatted like dd/mm/YYYY.
Well, I found a solution to my problem.
I don't know why but I just have set the "$date" format to : n/d/Y
and to wrap it between "#" in code like this :
"SELECT table.toto
FROM table
WHERE
table.date < #date#"

Converting string to date or getting the current date using Zend PHP Framework 2

I have a controller action in which I fetch the currrent date like following:
$fullDate=date("Y-m-d");
$date = strtotime($fullDate);
$viewModel->setVariable("currentDate",$date);
// Here I would like to pass the variable to the View and then compare it with another date ...
With the method above I'm getting this when I do var_dump($currentDate);
int(1463587200)
which is not what I was expecting... I need a date in format like this 2016-05-19 (year-month-day)... How can I do this the valid way???
This strtotime is converting your formatted date into a Unix time-stamp. just remove it.
$fullDate=date("Y-m-d");
$viewModel->setVariable("currentDate",$fullDate);
you'll be able to compare two dates in this format Y-m-d without converting to time-stamp.

Converting date to country requirements

I have a calendar in html form and I want to insert this date into MySQL. The default MySQL date is 0000-00-00. But in my country the format is DD/MM/YYYY. So what to do to fix it. Thank you. I am using PHP.
You must use one format in your HTML page, and another format in your database.
So, if you want to store a date like this '12/05/2008' into mySql, you must transform it like this:
$date = '12/05/2008';
$dateToStore = date('Y-m-d', strtotime(str_replace('/','-',$date)));
And if you wonder why, you need to replace the '/' with '-' to make php know that the first part of the data string is the day, and then the month (as I think is your case).
MySQL the date format is always YYYY-MM-DD. To convert it to another format, you need to manually convert the retrieved date to the desired format like
$displayDate=date("d/M/Y", strtotime($mysqldate));
Method 1
You cant insert into DD/MM/YYYY format. Instead while rendering it in view file you can change into desired format.
<?php
$date = $result['db_date']; // I ASSUMED YOUR DB FIELD IS db_date
$desiredFormat = date('d/m/Y', strtotime($date)); // CONVERTING INTO YOUR FORMAT
echo '<pre>'; print_r($desiredFormat); // DISPLAYING IT
?>
Method 2
You can retrieve from database in your desired format using below
SELECT *, DATE_FORMAT(YOUR_DATE_FIELD, "%m/%d/%Y") AS date FROM YOUR_TABLE;
Use MySQL STR_TO_DATE
Try this mysql query :-
INSERT INTO `table`(`date`) VALUES (STR_TO_DATE('10/10/2015', '%d/%m/%Y'))

Datetime format check in php

How would i go about checking to see if an auction has expired in my database? I have a datetime column in MySQL that i believe is of the following format: YYY-MM-DD hh:mm:ss. If this is the case would the following check work - i.e. want to select only expired auctions from the table in the database...
<?php
//Some code
$auctioncheck = mysql_query("
SELECT * FROM auction WHERE ($date_time > finish_time)
");
?>
While "finish time" is a column in the database of the above cited format. Presuming this works how actually do i get the current date into the same format? If anybody knows i would very grateful cheers. Even more so if the above query wouldn't work and something else is required. Thanks again.
Oh and of course i would define the date_time variable to start with
Do you actually need the $date_time variable? The easiest way to do this would be SELECT * FROM auction WHERE finish_time < NOW(). That way you'll get your results and don't have to set the date from PHP.
You can use
<?php
//Some code
$date_time=strtotime($date_time);
$auctioncheck = mysql_query("
SELECT * FROM auction WHERE ( $date_time > UNIX_TIMESTAMP(finish_time))
");
?>
This solution:
// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
is from this post: PHP Zend date format
and here's another solution: how to format a Date in MM/dd/yyyy HH:mm:ss format in javascript?
and to do it in php:
How to get the current date and time in PHP?
The function date is made for it:
// Get the current date
$date_time = date("y-m-d H:i:s");
Look at the documentation page to see other format flags ;)

Categories