-3

So i have a small issue. I am using jquery datepicker to collect dates. While users enter data, they see the following format mm/dd/yy But I would like to convert that to m-d-Y in Php.

Please help. Thanks

John Kariuki
  • 4,966
  • 5
  • 21
  • 30
  • 1
    possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – scrowler Jul 15 '14 at 21:35

4 Answers4

2
$date = DateTime::createFromFormat('m/d/y', $time);
echo $date->format('m-d-Y');
Scopey
  • 6,269
  • 1
  • 22
  • 34
  • +1 for actually using Date functions. Future proofing and considering other possibilities is important, and these other answers will only provide more work in the long run when that date format needs to change or be modified further. – scrowler Jul 15 '14 at 21:37
0

To "change date from m/d/Y to m-d-Y" you can just use str_replace

$date = '03/13/2013';
$date = str_replace('/', '-', $date);

If you need to change the format ie from yy to Y, try this:

$date = '03/13/13';
$date = date('m-d-Y', strtotime($date));

See this demo


References:

str_replace(), date(), strtotime()

Mark Miller
  • 7,442
  • 2
  • 16
  • 22
  • I tried that but i get this error @Mark M Fatal error: Call to a member function format() on a non-object in /home2/fairacre/public_html/resource-libs-files/classes/payments/booking.php on line 216 – John Kariuki Jul 15 '14 at 21:35
  • 1
    @JohnKariuki there is no call to `format()` in this answer, so your error is elsewhere – scrowler Jul 15 '14 at 21:39
  • @JohnKariuki Not sure where you are getting `format()` from. It's not in my answer or your question. Maybe you posted your comment on the wrong answer? – Mark Miller Jul 15 '14 at 21:41
0
$input = 'mm/dd/yy';
$a = str_replace('/', '-', $input);
echo $a;  // outputs mm-dd-yy
scrowler
  • 24,273
  • 9
  • 60
  • 92
kjm
  • 1
  • 2
    **Welcome to StackOverflow!** Please provide a little more information, background, references etc with your answers to avoid simply supplying a "code only" answer – scrowler Jul 15 '14 at 21:36
0

You have a couple of options.

  1. In jQuery's datepicker, use the altFormat setting to show the user a date in one format (mm/dd/yy), but send the date in a different format (m-d-Y)
  2. In PHP, convert the date, for example:
$incomingDate = "05/21/1979";

$newDate = date( "m-d-Y", strtotime( $date ) );

echo $newDate;
scrowler
  • 24,273
  • 9
  • 60
  • 92
Mike Willis
  • 1,493
  • 15
  • 30