Asked  7 Months ago    Answers:  5   Viewed   38 times

When I pull the date out of the db, it comes back like this:

2009-10-14T19:00:00

I want to format it in two different ways...

The first: F d, Y The second h:m (12 hour format)

Everything I try returns December 1969... Help?! I feel so confused...

 Answers

81

Normally the code is just:

echo date('F d, Y h:mA', strtotime('2009-10-14 19:00:00'));

Note that if strtotime() can't figure out the date, it returns the time as 1/1/1970 00:00:00 GMT.

Wednesday, March 31, 2021
 
themihai
answered 7 Months ago
71

Is your problem in getting the date from MS SQL as a date/time, or setting the Excel date?

There is a whole section of the PHPExcel documentation that explains the use of the PHPExcel_Shared_Date::PHPToExcel($PHPDate) and PHPExcel_Shared_Date::FormattedPHPToExcel($year, $month, $day, $hours=0, $minutes=0, $seconds=0) helper methods for converting PHP dates to an Excel datetime stamp value that you set as the cell value, and then you apply a number format mask of one of the date masks such as PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2 to that cell

Instead of

$t_year     = substr($xls_column_datas["colname"],0,4);    
$t_month    = substr($xls_column_datas["colname"],5,2);    
$t_day      = substr($xls_column_datas["colname"],8,2);
$t_format   = '=date('.$t_format.')';
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($data_column_num, $data_row_num, $t_format );
$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($data_column_num, $data_row_num)->getNumberFormat()->setFormatCode('[$-C09]d mmm yyyy;@');

try setting

$t_year   = substr($xls_column_datas["colname"],0,4);
$t_month  = substr($xls_column_datas["colname"],4,2);  // Fixed problems with offsets
$t_day    = substr($xls_column_datas["colname"],6,2);
$t_date   = PHPExcel_Shared_Date::FormattedPHPToExcel($t_year, $t_month, $t_day);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(
    $data_column_num, $data_row_num, $t_date 
);
$objPHPExcel->getActiveSheet()
    ->getStyleByColumnAndRow($data_column_num, $data_row_num)
    ->getNumberFormat()->setFormatCode(
        PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX14
    );
Wednesday, March 31, 2021
 
Vlad
answered 7 Months ago
72

DateTime::createFromFormat and date_parse_from_format have been added in PHP 5.3 because there was a high demand for that feature, especially from developpers who code for users who don't use US date/time formats.


Before those, you had to develop a specific function to parse the format you were using ; with PHP < 5.3, what is generally done is :

  • Decide which format will be accepted by the application
  • Display some message saying something like "your input should be JJ/MM/AAAA" (French for DD/MM/YYYY)
  • Check that the input is OK, regarding to that format
  • And parse it to convert it to a date/time that PHP can understand.

Which means applications and developpers generally didn't allow for that many formats, as each format meant one different additionnal validation+parsing function.


If you really need that kind of function, that allows for any possible format, I'm afraid you'll kind of have to write it yourself :-(

Maybe taking a look at the sources of date_parse_from_format could help, if you understand C code ? It should be in something like ext/date/php_date.c -- but doesn't seem to be that simple : it's calling the timelib_parse_from_format function, which is defined in ext/data/lib/parse_date.c, and doesn't look that friendly ^^

Friday, May 28, 2021
 
Palladium
answered 5 Months ago
87
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String yyyyMMdd = sdf.format(date);

Reference: java.text.SimpleDateFormat

Update: the question by The Elite Gentleman is important. If you start with a String, then you should first parse it to obtain the date object from the above example:

Date date = new SimpleDateFormat("dd MMM yyyy").parse(dateString);
Thursday, August 5, 2021
 
maciekm
answered 3 Months ago
22

Three possible explanations:

  1. your default locale is incompatible with the input date - e.g. it can't understand Sep as a month name
  2. there's something wrong with the input string, or
  3. t is the wrong type (e.g. java.sql.Date instead of java.util.Date, or some other type altogether), or is not declared.

You should include details of the exception in your question to figure out which it is, but here's a working example using basically your own code, with the addition of a specific Locale.

SimpleDateFormat ft = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US);
java.util.Date t=ft.parse("Sep 16, 2015 10:34:23 AM");
ft.applyPattern("MM.dd");
System.out.println(ft.format(t));

output:

09.16
Monday, August 16, 2021
 
Yarin
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :