Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php datetime

Posted on 2016-12-01
6
Medium Priority
?
31 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 30

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 400 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 53

Assisted Solution

by:Ryan Chong
Ryan Chong earned 400 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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 400 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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
LVL 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 400 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 111

Accepted Solution

by:
Ray Paseur earned 400 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

730 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