Solved

php datetime

Posted on 2016-12-01
6
30 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 100 total points
ID: 41908354
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 52

Assisted Solution

by:Ryan Chong
Ryan Chong earned 100 total points
ID: 41908362
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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 total points
ID: 41908439
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 100 total points
ID: 41908492
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 110

Accepted Solution

by:
Ray Paseur earned 100 total points
ID: 41908571
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
ID: 41909133
Thank you all
0

Featured Post

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

617 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