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.
2 7446
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. -
-
-
$date = "25/10/2010 19:34";
-
-
$ts = strtotime(
-
end(explode('/',current(explode(' ',$date,2)),3)).'/'.
-
next(explode('/',current(explode(' ',$date,2)),3)).'/'.
-
current(explode('/',current(explode(' ',$date,2)),3)).' '.
-
end(explode(' ',$date,2)));
-
-
echo date("r",$ts);
-
-
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
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 - list($dd,$mm,$yy) = explode('/',$date);
-
$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 - 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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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?
|
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...
|
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...
|
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...
|
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++) {...
|
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...
|
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";...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |