6

I have a form requiring date in dd/mm/yyyy format and I've tried to convert it to a timestamp with strtotime() function

but I've seen it works only if you fill the form with date written like dd-mm-yyyy

how could i solve? I don't know abroad but here in Italy nobody writes dates like that dd-mm-yyyy

Bora
  • 10,529
  • 5
  • 43
  • 73
GabAntonelli
  • 757
  • 2
  • 9
  • 26
  • 2
    Why not create a [DateTime](http://www.php.net/manual/en/class.datetime.php) object instead, using [createFromFormat()](http://www.php.net/manual/en/datetime.createfromformat.php) – Mark Baker Sep 12 '13 at 09:58
  • possible duplicate of [Convert this to DateTime?](http://stackoverflow.com/questions/15104397/convert-this-to-datetime) – Glavić Sep 12 '13 at 11:06
  • possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – vascowhite Sep 30 '13 at 15:40

4 Answers4

28

Stop using strotime(), date(), time() etc. functions, start using DateTime.

Perfect solution for all types of known input :

$dt = DateTime::createFromFormat('d/m/Y', $yourDMYinput);
echo $dt->getTimestamp(); # or $dt->format('U');

And why do you need timestamp for?

Why explode, replace or even cut string??? Just use createFromFormat.

Q1: Because its not supported below PHP 5.3.

There is v5.5 already out. I think answers should be for the latest code, not for code from the year 2006. Yes v5.2 was released in 2006 and v5.3 in 2009 (see release dates). If OP requests old code, find it on SO, and close this question with duplicate; don't answer it.

Q2: Because I don't know DateTime.

Learn it.

Q3: Because I can do more with strotime(), date(), time() etc.

Show me one thing that this functions can do, and DateTime cannot.

"Deprecated" code examples :

$input = '12/09/2013'; # dd/mm/yyyy

$new = substr($input,6,4)."-".substr($input,3,2)."-".substr($input,0,2);
$new = strtotime($new);
echo "$new\n";

$m = explode('/', $input);
$new = mktime(0,0,0,$m[1],$m[0],$m[2]);
echo "$new\n";

$new = str_replace("/", "-", $input);
$new = strtotime($new);
echo "$new\n";

preg_match('~^(\d+)/(\d+)/(\d+)$~', $input, $m);
$new = strtotime("$m[3]-$m[2]-$m[1]");
echo "$new\n";

$m = sscanf($input, '%d/%d/%d');
$new = strtotime("$m[2]-$m[1]-$m[0]");
echo "$new\n";

If you have another example of deprecated code, edit this answer and add it ;)

Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • 2
    I'm down voting this answer as, although I largely agree with your points, this is not the forum for a rant. Less rant, more answer please. – vascowhite Sep 20 '13 at 16:59
  • @vascowhite: just lol, where do you see raging talk? If you haven't noticed I have given 6, yes 6, ways/answers to solve his problem. And **ALL** of this methods are way better that the answer which has +1 ;) If this is raging, then what is this > http://stackoverflow.com/a/1732454/67332 ??? – Glavić Sep 20 '13 at 17:04
  • You can't use this before 5.2. – Zack Zatkin-Gold Nov 07 '13 at 06:24
  • @zzatkin: no, DateTime::createFromFormat cannot be used before < 5.3.0; that's why they are 5 different solutions at the end of the answer, that will work even on 4.x. – Glavić Nov 07 '13 at 09:37
  • @Glavić : thanks for the answer how do i change this unix timestamp to same date which i gave as input – Hitesh Sep 04 '14 at 10:11
  • @hitesh: just use '->format()' method on DateTime object – Glavić Sep 04 '14 at 18:31
0

Why not use str_replace?

$timestamp = strtotime(str_replace("/", "-", $dateString));

Or better yet, convert the string into YYYY-MM-DD before you insert.

Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
0

Your question is not very clear.

If you receive from a form the date in format "dd/mm/yyyy" and you need to convert it in "yyyy-mm-dd" you can simply use substr:

$date_eng=substr($data_ita,6,4)."-".substr($data_ita,3,2)."-".substr($data_ita,0,2);

and

$data_ita=substr($date_eng,8,2)."/".substr($date_eng,5,2)."/".substr($date_eng,0,4);
Paolo Gibellini
  • 310
  • 13
  • 21
  • it looks to me that with strtotime() it works also in dd-mm-yyyy format – GabAntonelli Sep 12 '13 at 10:13
  • Yes, strtotime() seems to work also with "dd-mm-yyyy". I thought your question was also related to switching between european and us format. – Paolo Gibellini Sep 12 '13 at 10:23
  • @gabs: strtotime() _seems_ to work also with "dd-mm-yyyy", but be aware that "each parameter of this function [strtotime] uses the default time zone unless a time zone is specified in that parameter" (from [documentation](http://php.net/manual/en/function.strtotime.php)). Changing timezone of the server can lead you to unexpected behaviours. Perhaps it's better use a DateTime object as suggested by mark-baker. – Paolo Gibellini Sep 12 '13 at 10:30
  • Why why why why why explode, replace or even cut string??? Just use http://www.php.net/manual/en/datetime.createfromformat.php – Glavić Sep 12 '13 at 11:15
  • @Glavić: as said, it's better use a DateTime object – Paolo Gibellini Sep 12 '13 at 11:24
  • But why do you teach newbies "old" (if I can call this old) school? – Glavić Sep 12 '13 at 11:32
  • Because this way is always available, even with version of PHP older than 5.3 (and yes, there are a lot of not updated servers in the world, and sometimes you must use _ancient_ technology), and not last because if a newbie understands how to perform an operation with simple statements, then it can use more complex objects with a deeper comprehension. But this is just my opinion. – Paolo Gibellini Sep 12 '13 at 11:45
  • Are you sure that this is always available? What if OP has php v1-v3? So what if they are servers with old php? Will we write php v4 code when php v26.4.x will be out? And using 4 years old code is not latest for me. – Glavić Sep 12 '13 at 11:53
0

Quite lengthy.

$get_array = explode('/', 'dd/mm/yyyy');

$day = $get_array[0];
$month = $get_array[1];
$year = $get_array[2];
$time_Stamp = mktime(0,0,0,$month,$day,$year);
Parixit
  • 3,829
  • 3
  • 37
  • 61
  • 1
    Why why why why why explode or replace string??? Just use http://www.php.net/manual/en/datetime.createfromformat.php – Glavić Sep 12 '13 at 11:09
  • @Glavić Because its not supported below PHP 5.3 – Parixit Sep 12 '13 at 11:10
  • There is 5.5 already out. I think answers should be for the latest code, not for code from year 2006; [yes php5.2 was released in 2006 and 5.3 in 2009](http://en.wikipedia.org/wiki/PHP). If OP needs old code example, he needs to tell that. – Glavić Sep 12 '13 at 11:14
  • @Glavić you're right and I +1 your answer because of that, but let others answer without the repetitive criticism huh? That's why you appear to be ranting a little. As always in php, there is more than just one right answer and more than one functional perspective. Respect others. :) – Cassandra May 09 '17 at 18:58