This is easy and i did search for it and I gotn really complicated answers or things that didn't match my question exactly it's really simple
I have the date
$day = "Jaunuary 8th 2014 5:00pm";
I got this date from a POST from a forum, the user can only select a few dates and time so the value is controlled.
What is the best way to INSERT day and time?
like this? 04-18-2011 or 20091228 for day and what about time?
When I retrieve this information the goal is so i can sort it by time and date so that i can print it out in ORDER OF date
I should probably INSERT date and time together as one variable correct?
Why complicate things?
MySQL has the syntax of "YYYY-MM-DD HH:MM:SS" for timestamps. Luckily, PHP's date function can handle this quite well:
// the POST variable you retrieved, converted to a time via strtotime(), then converted to a date via date()
$appt = date('Y-m-d H:i:s',strtotime($_POST['appointment_datetime']));
PHP's date and strtotime functions are smart enough to interpret everything correctly and translate it to the format you need to INSERT into MySQL:
"INSERT INTO someTable (AppointmentDate) VALUES ('$appt');"
Then, when you retrieve it from the DB, you reformat it (showing the basic mysqli syntax rather than using proper binding, just for ease of explanation):
$appt = mysqli_query($link,"SELECT AppointmentDate FROM someTable;");
while($apptRow = mysqli_fetch_array($appt,MYSQL_ASSOC)){
echo date('F j, Y g:i a',strtotime($apptRow['AppointmentDate']));
}
This will echo the "plain english" version of the dates. This is obviously simplistic, you would likely capture it into a variable and display appropriately, but you should get the gist. You can consult the PHP date function documentation to learn the appropriate symbols you can use, if you want to have a different format.
Hope this helps. :)
Store a Unix Timestamp and then you can display it in any format you like
$ts=time(); // Store that value in an INT (11)
Then you can display it like
echo date("F j, Y, g:i a",$YourStoredTimestamp); // eg March 10, 2001, 5:16 pm
Related
I am using a now() code to enter just a date into the database. The date appears as Year/Month/Day (yyyy/mm/dd).
In my PHP code I have been using:
$news_date=$row['news_date'];
to recall the date on my site, I was wondering, instead of the date all showing at once, is there a way to add a separate value for day, month and year? So I could recall 'day' separate and so forth? Obviously without changing the database to have 3 different inputs, there must be an easier way? I am fairly new to PHP and MySQL so if someone could help me out with an example that would be handy! Thanks.
PHP has a very powerful function called strtotime() that intuitively converts next to any datestamp into a UNIX-based timestamp, which you can then format any way you want.
$news_timestamp = strtotime($row['news_date']);
Now you can use PHP's date() function to output this timestamp in all sorts of ways.
print 'Today is day number '.date('d', $news_timestamp); // 20
print 'The month is '.date('F', $news_timestamp); // March
print 'Post timestamp: '.date('l, F d, Y # h:ia', $news_timestamp); // Thursday, March 20, 2014 # 03:46pm
please i need your help i created a comment box in my page, stored inside phpmyadmin with time type DATETIME. the problem am having is the time always display in 24 hour format and i want it to display in 12 hour format (PM/AM) and be stored inside mysql. i have tried using date() function at the same time i used date("y-m-d H:i:s") instead of now())function but the result i keep on getting is in 24 hour format
see the code
$insert = mysql_query("INSERT INTO test (name,comment,whenadded) VALUE ('$name','$comment', now())");
With this code i get the result in 24 hour time format.
whenadded is the DATETIME variable name.
thank you in advance.
You want to store the date as DATETIME in the MySQL DB, thats good practice.
For output, use PHP's date() function. Look at this answer. Or you use MySQLs date_format() function.
SELECT date_format(whenadded, 'Y-m-d h:i') AS my_date FROM ...
The php documentation should help http://www.php.net/manual/en/function.date.php
what you are looking of is date(y-m-d g:i a) wich will give something like "2013-12-30 4:38 pm"
Let the mysql decide its date format, it's mostly irrelevant for you.
What you need, is to properly format your output data, like:
echo date("y-m-d h:i:s A", strtotime($date));
Where $date is the variable you get from MySQL.
In no particular order:
phpMyAdmin is not a database engine. MySQL is.
Dates are not stored in any particular format. You give format when you convert them to strings.
The mysql_... legacy extension is deprecated, insecure, triggers a notice in latest PHP versions and will be removed.
Your code is probably vulnerable to SQL Injection.
The H format code means: 24-hour format of an hour with leading zeros.
It's good to save the data within 24Hours Format, but you can show it within 12Hours plus am/pm
date("d/m/Y - g:i A");
Hello i am trying to add a date time from PHP into my MySQL database, in the database I have tried date time and time stamp and I have tried it with and without current_time set as the default option, in my php I have the following.
date_default_timezone_set("Europe/London");
$date = date('m/d/Y h:i:s a');
My hope was to just add the $date into the value's part of the upload query however in all cases it comes back as 0's, it echo's the date fine, however it uplaod's 0's, any help is appreciated, Thanks.
'm/d/Y h:i:s a is not a standard MySQL datetime format so unless you are storing it as varchar/char you will get the results you are seeing.
Your options are to:
Store the date in a standard format (datetime Y-m-d H:i:s, timestamp) and convert it to whatever format you want during the query (recommended)
Store it as a string but lose all of the datetime functionality MySQL offers. Losing this functionality will make working with dates in your queries and PHP very painful and is not recommended to do.
I am using PHP and mysql and using either Date or DateTime to save dates in mysql database. On site I have been displaying dates the way they are saved in database.
But now I want to show dates EVERYWHERE on site using one format:
April 17 2013
or
April 17 2013 12:20:50
I know I can use date and strtotime functions to display dates in above format. However there are a lot of places where I have date displaying code. So I am looking to automate the process where my current code works and displays dates in above format.
Any idea of how mysql trigger or some php magic could be created that converts all dates run through SELECT query automatically without changing my sql or php code since I have a lot of places in my code and it would be overkill to change code at all places?
For Example:
Date Saved in DB: 2013-04-16 12:41:26
SELECT QUERY: SELECT * FROM myTable
PHP: echo $row->dated; displays 2013-04-16 12:41:26
I want that without changing my php code, dates should be shown in above mentioned format globally on whole site.
Any ideas please how it could be achieved ?
You can directly format in via query using DATE_FORMAT()
SELECT DATE_FORMAT(myDate, '%M %d %Y %h:%i') myDate
FROM TableName
SQLFiddle Demo
and echo in your PHP: $row->myDate
MySQL Trigger doesn't project values and It is only fired during CrUD operations.
I would like to suggest you an alternative approach which i love to use.
You should use the epoch time. An epoch time is basicly the number of second that has passed since 1 January 1970
One if the benefits i love is that it is very easy to calculate
differences in time since you are just dealing with number of
seconds and not a complicated format such as sec min hrs
Another benefit is that it is very easy to store since its a
integer so you can store it in a sql db and have your php code understand it without worrying about the format and things like that.
In php, if you use the time() function, it will return the epoch time.
And if you ever want to display it in a user friendly way. you can use the following code:
$epoch = time();
$dt = new DateTime("$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00
As you can see, the format of the date is very flexible and thus easy to use.
A nice example to find the date 1 week ago:
$epoch = time() - 604800; //604800 seconds = 7 days
$dt = new DateTime("$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00
i'm having trouble getting the date to be imported into mysql from my form.
my form is validated so that the input will always be dd/mm/yyyy
i'm currently using
$date = date('Y/m/d', strtotime($_POST['night_attending']));
this takes the value from the form and assigns it the year/month/day
the problem is that its reading my form as mm/dd/yyyy
i can swap the code to 'Y/d/m' which will put it into my database the right way round but it will stop working if the day is past the 12th as it still believes that it is the month
i have tried using
date_default_timezone_set('Europe/Dublin');
and
date_default_timezone_set('Europe/London');
but it makes no difference
i'm contemplating just using the mm/dd/yyyy format on my form, but this isn't great as it's a uk site.
has anybody encountered this problem? i'm sure it must be comman and there must be a simple answer that i'm missing.
thanks
alsweet
There is no means to set MySQL's date format - the options exist, but they aren't enforced/used.
I don't recommend storing the dates as VARCHAR in order to maintain your format -- use the MySQL format, and work with MySQL date functions (IE: STR_TO_DATE, DATE_FORMAT) to output the format you want for screen.
Be aware that dates will be dependent on the timezone of the host MySQL is on - you might want to consider using epoch timestamps instead, depending on your needs.
This will give you the mysql date format from UK date.
$timestamp = strtotime(str_replace('/', '.', '03/04/2011'));
$mysql_date = date('Y-m-d', $timestamp); // 2011-04-03
You can use strptime() to parse the date:
$dateArray = strptime('%m/%d/%Y');
or date_parse_from_format() on PHP 5.3.
EDIT: I saw this too in the comments for strtotime, might help.