Solved

php datetime

Posted on 2016-12-01
6
26 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 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 50

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 54

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 109

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hasing a url 16 28
php simple error message 4 24
Google Maps API and PHP 25 34
PHP PDO   How do I correctly remove record using DELETE FROM? 1 21
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

803 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