Home  >  Article  >  Backend Development  >  How to convert date to string in php

How to convert date to string in php

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-19 16:17:162095browse

In PHP, you can use "SimpleDateFormat.format" to convert date into String, the syntax is "SimpleDateFormat.format("Time|Format")"; the usage of format is to convert the current time format into the specified format.

How to convert date to string in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

How to convert date to string using php: you can use SimpleDateFormat.format converts date into String

SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Date date1 = null;
Date date2 = null;
String str1 = "2009-02-14 12:00:00";
String str2 = "2009年02月14日 12时00分00秒";
// String转Date:String 必须严格按照定义的格式
try {
date1 = format1.parse(str1);
date2 = format2.parse(str2);
} catch (ParseException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("date1= "+date1);
System.out.println("date2= "+date2);
//Date转StringString
str3 = null;
String str4 = null;
str3 = format1.format(date1);
str4 = format2.format(date2);
System.out.println("str1= "+str3);
System.out.println("str2= "+str4);

Recommended: "2021 PHP Interview Questions Summary (Collection)" "php video tutorial

The above is the detailed content of How to convert date to string in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn