Solved

php datetime

Posted on 2016-12-01
6
17 Views
Last Modified: 2016-12-01
My users enter a date on my web site, I save it to the mysql database.

They enter a date for 31.12.2016 but it saves to database like 2031-12-20 16:00:00

mydatabase table coloumn properties is set to "datetime"

I want to enter the date it to my database like this 31.12.2016

I post the data with this variable $sontarih

what do you suggest I should do?
0
Comment
Question by:Braveheartli
6 Comments
 
LVL 17

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 100 total points
Comment Utility
Why do want to do that?

In this case you have change the data type of the column from datetime to date..

If you want to remove time part from the date then you can use like below in the select list..

SELECT Date(YourColumnName) FROM Table

Basically what is your requirement now?

Hope it helps !!
1
 
LVL 49

Assisted Solution

by:Ryan Chong
Ryan Chong earned 100 total points
Comment Utility
They enter a date for 31.12.2016 but it saves to database like 2031-12-20 16:00:00

mydatabase table coloumn properties is set to "datetime"

I want to enter the date it to my database like this 31.12.2016
it depends on how do you set the variable $sontarih

can you post the script accordingly?

or else you can actually set a default date (or date + time) value automatically for your field so that you no need to set it in your php code.

like set the field's default value to CURRENT_DATE()

check this out for further info:

12.3.5 Automatic Initialization and Updating for TIMESTAMP and DATETIME
http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
1
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 total points
Comment Utility
They enter a date for 31.12.2016 but it saves to database like 2031-12-20 16:00:00
That is exactly what should happen.  Your desired format does not sort properly but the database format does.  The 'date' functions in the database won't work properly with a different format either.  You can use the PHP date() function to return the date in any format you want.  http://php.net/manual/en/function.date.php
1
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 100 total points
Comment Utility
How you want to display your dates is independent of how you store the date. The database expects the date to be submitted in a particular format - if you go outside of that the date will be mangled.

When you need to display the date - you take the date in the DB format and re-format it in the format you want to display it.

If possible you should consider using the ISO 8061 for your dates - it will make coding a lot simpler
1
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 100 total points
Comment Utility
First read these articles, then bookmark them.  Almost all of the date/time questions you will ever have are answered here.

https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html

https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html

Example here:
https://iconoun.com/demo/temp_braveheartli.php
<?php // demo/temp_braveheartli.php
/**
 * https://www.experts-exchange.com/questions/28986581/php-datetime.html
 */
error_reporting(E_ALL);

$input = '31.12.2016';
$outgo = date('c', strtotime($input)); // ISO-8601 STANDARD DATE
var_dump($input, $outgo);

Open in new window

1
 
LVL 1

Author Closing Comment

by:Braveheartli
Comment Utility
Thank you all
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now