0

I need to convert date string into number:

2012-Sep-01 to 2012-09-01

Any idea? Regards,

Wilf
  • 2,297
  • 5
  • 38
  • 82
  • 6
    What have you tried? I'm not a PHP person, but I'd look for date/time parsing and formatting libraries. – Jon Skeet Sep 20 '12 at 09:53
  • datetime.format()... but phpnet seams down.... – Mathlight Sep 20 '12 at 09:54
  • 1
    we would love to help those people who help themselves ... you are supposed to do some searcher first than ask – NullPoiиteя Sep 20 '12 at 10:00
  • @RegisteredUser, I've tried the research before and I knew that the date() format and strtotime() works fine this way but the problem is the dynamic value is not passed so I can't get the expected date. Sometimes some problem is not because I don't do the research but for some over-programmed makes me confused. So If you don't want to answer just be quiet. And thanks for those who make my simple question clear. And I'm really sure that those useful answer here will be values for someone else than yours. – Wilf Sep 20 '12 at 17:33

7 Answers7

6

This might work for you:

echo date("Y-m-d", strtotime($yourCurrentDateVat))'

PHP website is down at the moment, but here is some extra info on the date function.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
6

Try to use DateTime PHP class:

$date = "2012-Sep-01";
$result = DateTime::createFromFormat("Y-M-d", $date)->format("Y-m-d");
Dmitry Yaremenko
  • 2,542
  • 20
  • 31
4

Use strtotime():

echo date('Y-m-d', strtotime('2012-Sep-01'));
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
3
$date = DateTime::createFromFormat('Y-M-j', '2012-Sep-01');
echo $date->format('Y-m-d');

Actually stolen from the manual: http://us.php.net/manual/en/datetime.createfromformat.php

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

You should use strtotime(). More info on here. And then date() to create a new string. More info on that here.

$date = '2012-Sep-01'; //Sets your date string
$time_epoch = strtotime($date); //Converts the string into an epoch time
$new_date = date('Y-m-d', $time_epoch); //Creates a new string
Jacob Tomlinson
  • 3,341
  • 2
  • 31
  • 62
1
$timestamp = strtotime('22-09-2008');
$new_date = date("y-m-d", $timestamp);
Ben
  • 5,627
  • 9
  • 35
  • 49
0

Hi, try using this in PHP and you will surely get what you want, I was searching for the same but didn't found but later discovered I had the date-time formula used somewhere and modified it to get this.

$getdatetime=date_create('2020-May-21'); $dateconverted=date_format($getdatetime,'Y-m-d'); echo $dateconverted;