Solved

php date format

Posted on 2013-01-24
10
502 Views
Last Modified: 2013-02-08
I have recently updated by database date formats from xx/xx/xxxx to strtotime values (Thanks Ray P) but I still have an application which is working on the old format:

I have a text box on a php page which is sent to another page using GET
The original value in the text box is : 25/09/2012

The url sent is as follows:
?dte1=25%2F09%2F2012

I need to ba able to use a strtotime function  on this on the second page to convert to a time string:
ie
$newvariable = strtotime($_GET[dte1]);

The problem is that the resulting strtotime value is not correct. I can only assume that this is because of the way the string is encoded in the url string initially
Is hrere a way round this ?
0
Comment
Question by:doctorbill
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 8

Accepted Solution

by:
Barry62 earned 125 total points
ID: 38815717
$newvariable = strtotime(str_replace("%2","/",$_GET[dte1]));
0
 

Author Comment

by:doctorbill
ID: 38815890
Same problem:

If I use 11/10/2012 as an initial date:

With your suggestion I get 1352505600
The actual figure should be 1349910000

ps I know it should be 1349910000 because that is what has been created in my database when the 11/10/2012 was inserted using the strtotime function a while back
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38816016
I think you must have transposed some numbers.  The difference between the number you got with my suggestion and what you say the actual number should be is 2595600.  That translates out to 1 month.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38816020
Isn't that about
http://php.net/manual/en/function.strtotime.php#106105
... difference with American and European time format ?
0
 
LVL 18

Assisted Solution

by:ingwa
ingwa earned 250 total points
ID: 38816074
Might I suggest you read the following article I wrote about date formatting, especially when using a database. There are very simple sql statements you can use which will do all the formatting you need without the construction of lines of php code which will add to the execution time of your app:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3935-Working-with-Dates-and-Time-using-PHP-and-MySQL.html
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 38816426
Please see http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Please see http://www.laprbass.com/RAY_temp_doctorbill.php

<?php // RAY_temp_doctorbill.php
error_reporting(E_ALL);

$wrong = '25/09/2012';
$array = array_reverse(explode('/', $wrong));
$right = implode('-', $array);
var_dump($wrong);
var_dump($right);

$rdate = @date('r', @strtotime($right));
var_dump($rdate);

Open in new window

0
 

Author Comment

by:doctorbill
ID: 38817887
0
 
LVL 18

Assisted Solution

by:ingwa
ingwa earned 250 total points
ID: 38817895
Because strtotime translates to Unix Timestamp...the Unix Epoch time that started in 1970. When you feed just a date it will translate to the date and use a time of 0000 hours. It's not a great time format to use...did you choose this for field size? For 4 bytes you could use an actual mysql timestamp. Your mysql database has all the tools and features available for you to process your date stuff quickly and efficiently without bloating your application with many lines of extra code.

At the moment your database supports one type of format, however what happens if you need to take up hosting somewhere else in the world where your timezone will change, as will perhaps the date format. By using integers instead of timestamps, lines of code to hack a date into and out of mysql changing order of ddmmyyyy to yymmdd is setting yourself up for tons of work in the future. When you do move, by keeping timestamp formats your database will adjust itself accordingly...however with ints and translations you're going to have to reprocess that data, reformat it and put it back into the db.
0
 

Author Comment

by:doctorbill
ID: 38818174
I managed to get this to work:
$dt1 = strtotime(str_replace("/","-",$_GET[dte1]));

Thanks VERY much for all the wealth of information re. dates - I fully appreciate I need to read up on the subject for the future. I did not know that dates could demand so much attention
0
 

Author Closing Comment

by:doctorbill
ID: 38818182
Solved
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 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

15 Experts available now in Live!

Get 1:1 Help Now