473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to get strtotime() to recognise dd/mm/yy?

code green
1,726 Expert 1GB
I am reading a CSV file with dates in non-US format:
$date = 25/10/2010 19:34
which should to write to MySQL as 2010-10-25. The time element is not important.
I have only just noticed (after a couple of months) that $tmstmp = strtotime($date)
is returning false because strtotime expects a US style date.

Is there anybody that knows a way around this? I am suprised I have never encountered this problem so far.
I can only think of string manipulation to re-order the date format.
Nov 2 '10 #1
2 7446
dlite922
1,584 Expert 1GB
I think there's just no way. I believe it's built in to PHP. I'm not sure even if setting the region will influence it at all either, you can try it.

IMO string manipulation is your best reliable bet.

Here's one I did all on one line, quite inefficiently might I add.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. $date = "25/10/2010 19:34";
  4.  
  5. $ts = strtotime(
  6.     end(explode('/',current(explode(' ',$date,2)),3)).'/'.
  7.     next(explode('/',current(explode(' ',$date,2)),3)).'/'.
  8.     current(explode('/',current(explode(' ',$date,2)),3)).' '.
  9.     end(explode(' ',$date,2)));
  10.  
  11. echo date("r",$ts); 
  12.  
  13.  
That's definitely the wrong way to do it, but it's a challenge to anybody else to see who can do it on one line.

Dan
Nov 3 '10 #2
code green
1,726 Expert 1GB
Thanks for your reply Dan. It is nice to know the whole world has to kowtow to the US way of writing the date wrong.
Anyway, I worked on it yesterday and got it down to two lines of string manipulation
Expand|Select|Wrap|Line Numbers
  1. list($dd,$mm,$yy) = explode('/',$date);
  2. $date = sprintf('%s/%s/%s',$mm,$dd,$yy);
I 'borrowed' the list() idea from another site but sprintf was mine.
I did set about writing a generic function, something like
Expand|Select|Wrap|Line Numbers
  1. function formatDate(string $oldformat, string $newformat, string date, string delimiter)
But it quickly grew in size due to the permutations and whether a time element was involved
Nov 4 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Pjotr Wedersteers | last post by:
I'm using strtotime to get the timestamp for midnight. I have a statistics script for my pagecounter that displays hits since midnight. I also display the number of hours and minutes passed since...
1
by: peabody | last post by:
I'm trying to use the strtotime() function to manage sessions. But I get the following <?php print(time() . " - " . strtotime("+1 hour")); ?> outputs: 1097380666 - 1097308800
5
by: Leif K-Brooks | last post by:
PHP has a very nice strtotime function (http://php.net/strtotime) which converts a date/time in virtually any format into a timestamp. Does Python have a similar function?
7
by: Rithish | last post by:
Hello. I noticed a strange thing while using strtotime() and date() functions in combination to generate from MySQL into a readable format. By default, the MySQL date field will be 0000-00-00...
18
by: windandwaves | last post by:
Hi Folk I love the strtotime function, because I can make a date field in a form and just tell my users to enter whatever they want and as long as it is a date, it will work. HOWEVER, this...
6
by: Brian Kendig | last post by:
I'm working with dates in several formats including 'yyyy MMM dd', but strtotime doesn't recognize this format and returns FALSE. Is there a direct way to convert times from this format into...
5
by: cla | last post by:
I'm using this code on an application to track football schedules: ---- $season = '2005'; $basedate = strtotime('this friday', strtotime('31 August '.$season)); for($d=0;$d<=31;$d++) {...
5
by: Chris | last post by:
I am trying to output Monday of the current week i.e. if Monday is the 8th I want to display 'Monday 8th' for any date between Monday 8-14th. I would appreciate any help, the code below is...
9
by: Erwin Moller | last post by:
Hi group, I have been using strtotime a lot in my code. I wonder if I made a mistake in my thinking. :-/ Here follows a stripped down example. consider some dates: $date1 = "2008-02-23";...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.