Datetime format check in php - 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 ;)

Related

How to get time with month name,date and hour?

i have used Now() and it stores something like "2017-01-10 19:28:58" in database which is the current time of user's device.
But i want it like January 10 at 7:28pm . how to do it in simple way. please help
You can use PHP date function with its formatting options
<?php
echo date("F d \a\t g:ia");
If you want more detailed formatting please visit the PHP manual here. You can find everything you need with detailed examples.
you could use the PHP date function
date(F d \a\t\ g:ia);
Your post is ambiguous, but it sounds like you are referring to the SQL NOW() function. The format of the data stored could be a DATETIME or TIMESTAMP or a combination of DATE and TIME columns. The format you get when you retrieve this value from the database depends on that data storage format, your query, and how the value is dealt with when it gets into PHP.
If you want to reformat it using SQL, consider a function like DATE_FORMAT. Assuming your column with the date is called my_column, here's a sample query.
SELECT DATE_FORMAT(my_column, '%M %e at %l:%i%p');
EDIT: you can also use DATE_FORMAT on the result of the NOW() function:
SELECT DATE_FORMAT(NOW(), '%M %e at %l:%i%p');
You might have to tweak the second parameter to get the date format you want.
If your date is stored as a string (VARCHAR or whatever) in your database, then you would need to convert it to a timestamp or datetime first and then use the PHP date function to output the variant you want. Assuming $row is a record from your data table:
$date_string = $row["my_column"];
$stamp = strtotime($date_string); // NOTE that this will assume some timezone
if (!$stamp) {
die("Could not create a timestamp from the date");
}
echo date("F j \a\t g:ia);
You could also use PHP'S DateTime functions which are more modern, if somewhat verbose in usage.

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'))

equivalent of mysql CURRENT_DATE() function in PHP

Hi i'm inserting a date entry into a field that has a Type Timestamp and field default CURRENT_TIMESTAMP ...my insert value will look like this
'.($data[16] == '' ? CURRENT_DATE() : $data[16]).'
how would I format the CURRENT_DATE() to be 30-Nov-10 to match what $data[16] format would be if not present. Also should I change the Field Type to Date and not Timestamp?
date($format,strtotime(CURRENT_DATE)) works for me.
CURRENT_DATE() is a MySQL function. You could use it as a string literal:
$sql_date = ($date == '') ?
"DATE_FORMAT(CURRENT_DATE(),'%d-%b-%y')" : "'$date'";
mysql_query("UPDATE foo SET date=$sql_date");
This assumes that $date is safe for SQL insertion.
Or you could just use PHP:
if (!$date) $date = date('%d-%M-%y');
mysql_query("UPDATE foo SET date='$date'");
Personally, I would generally use the PHP solution as it makes it easier to use the same query with the same safe parameter placeholders (not used in this example) regardless of how you build the date.
Also should I change the Field Type to Date and not Timestamp
If the time is irrelevant, then it should be a date field. However, if it's a date/timestamp field (and not a text field), you should be inserting in the YYYY-MM-DD format, and the above code is useless.
That is, if the field is text, then the above code could be useful. However, if you are storing simply a date into a single field, you should use a date field, In that case, the format you insert should always be in YYYY-MM-DD. When you retrieve the data, you can format it in the way you want for display.
Well without any further information
all i can tell you is
you should look into the function date and it's second arg
where you put a timestamp which you could create with mktime alt. strtotime
This question doesn't make sense.. I'm assuming you want to output the current date.. in which case you can change the format with the following:
$today = date("M/d/y");
also, time() will output the current date to the second in a unix timestamp
$today = time();

Change display format of date and time field in MySQL PHP

My select statement in PHP is
"select * from table";
I use the following PHP statement to display date & time of MySQL field.
<?php
echo $row['mdate'];
?>
The result come like this
2010-03-09 16:59:18
I want to view the result in the following format
09-03-2010 16:59:18
and I want to view the result in the following format
09-03-2010 4:59:18 PM
without defining any extra function. I can only modify my echo statement.
<?php echo $row['msgdate']; ?>
or
I can also modify my select statement.
See date_format():
select *, date_format(mdate, '%d-%m-%Y %H:%i:%s') AS formated_date from tabl;
And use formated_date in jouw php-code.
You can do the formatting directly in the database as Frank Heikens shows in his answer, or you can do it in PHP. Convert the mySQL date value to a UNIX timestamp using
$timestamp = strtotime($row["mdate"]);
then you can use all options of date() to format it, for example:
echo date("Y-m-d H:i:s", $timestamp); // returns 09-03-2010 16:59:18
both approaches are equally valid; I personally like to modify the value in PHP.

How to use the mysql "LIKE" syntax with a timestamp?

I want to allow my users to search the database for a row that was submitted on a certain day. The date is entered into the field in the database using the date() function which is great, but when i use the php strtotime function, of course the dates are not exactly the same.
Is there some clever mysql function that can help me with this?
I had an issue with this before.
You're best to generate a start and end date then use the BETWEEN function instead.
So something like this:
$start_date = "2009-01-01 00:00:00";
$end_date = "2009-01-01 23:59:59";
$sql = "SELECT * FROM table WHERE date BETWEEN '$start_date' AND '$end_date' AND id = 'x';
Of course you would just pull your dates from the DB using strtotime and append the time stamps - depends how you used date()
Hope this helps :)
You can use MySQL's DATE() function to extract date part of the timestamp:
select ... from table ... where DATE(date_column) = '2010-01-25';
If you have problem submitting '2010-01-25' from PHP, you can use PHP's date function with 'Y-m-d' as parameter to only get the date part.
$d = date('Y-m-d', strtotime(...));
Looking at your question closely, it seems you'll need both of those. First use PHP's date function to get only the date part and then use MySQL's date to match only those records.
PHP:
$timestamp_from_php = strtotime('December 25, 2009');
SQL:
select
`fields`
from
Table t
where
date_format('Y-m-d', t.`datetime_field`) = date_format('Y-m-d', '$timestamp_from_php')

Categories