Solved

php datetime

Posted on 2016-12-01
6
29 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 28

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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 57

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
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. . .
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…
The viewer will learn how to dynamically set the form action using jQuery.

752 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