當前位置:首頁 » 編程語言 » phpstringtodate

phpstringtodate

發布時間: 2023-05-17 05:19:53

php string 類型轉換成 time 類型的問題

$time = time()返回的是一個整數。
$date = date("Y-m-d H:i:s",$time); 返回2009-09-09 11:29:33格式的時間
strtotime($date);把時間字元串重新轉換成整數

計算的話直接用time()生成時間比較好。精確到秒。

如果只要時間的話 date("H:i:s",$time);就可以了啊,如果是兩個時間差值,比如12334秒你要算成時間,只有一步步計算

❷ php中如何將string類型轉換為date類型插入到資料庫中的date類型欄位中,incorrect date value啥意思

1、首先介紹一下將String類型轉為Date類型的方法。需要導入java.text.SimpleDateFormat類。下面舉一個例子,比如有一個字元串 「2018-08-24「,想要轉為Date類型,代碼如圖所示。

❸ PHP入門教程之日期與時間操作技巧總結(格式化,驗證,獲取,轉換,計算等)

本文實例講述了PHP日期與時間操作技巧。分享給大家供大家參考,具體如下:
Demo1.php
<?php
//驗證時間
//checkdate()
1.月份
2.日
3.年
//checkdate()
判斷這個日期是否是合法的日期
//不合法的日期,試一試
if(checkdate(7,16,2010)){
echo
'這個日期是合法有效的';
}else{
echo
'這個日期是非法的。';
}
?>
Demo2.php
<?php
//date
--
格式化一個本地時間/日期
//date(),
徹底研究一下
//date()
可以存放兩個參數,第一參數是日期和時間的格式化,[第二參數是時間戳]
//Y
表示四位數的年份,
y表示二位數的年份
//M
表示英文的月份縮寫,m
表示阿拉伯數字的月份
//D
表示英文下的星期幾縮寫,d
表示阿拉伯數字的日
//第一個參數的格式化可以放一些無關緊要的字元串
//只要無關緊要的字元串不再
format
的目錄里,就不會被識別
//echo
date('現在的日期是:Y-m-d');
//現在的日期是:2015-04-20
//時分秒
=
H
表示24小時制的小時,
//明明是
19
,為什麼顯示
11
點呢,東八區,差
8
個小時
//現在沒有經過任何設置,所以時間在默認時區上
//echo
date('現在的日期是:Y-m-d
H:i:s');
//重點是年月日,時分秒
echo
date('r');
echo
date('現在的日期是:Y-m-d
H:i:sa');
?>
Demo3.php
<?php
//取得當前的時間,返回一個數組
//"sec"
-

Unix
紀元起的秒數
//"usec"
-
微秒數
//"minuteswest"
-
格林威治向西的分鍾數
//"dsttime"
-
夏令時修正的類型
//print_r(gettimeofday());
//第一數組的元素就是時間戳
//gettimeofday()
就是取得的當前時間的時間戳
//$a
=
gettimeofday();
//sec
取得當前時間的時間戳
//轉換成人可以看得懂的時間
//第二個參數,對於本例來講,放與不放,是一樣的。
//echo
date('Y-m-d
H:i:s',$a['sec']);
print_r(gettimeofday(0));
echo
gettimeofday(1);
?>
Demo4.php
<?php
//將時間戳轉換成人可以看的懂的時間
//date()
函數的第二個參數就是時間戳
//如果第二個參數省略了,那麼就返回當前時間
//如果第二個參數沒有省略,那麼就返回那個時間戳的時間
echo
date('Y-m-d
H:i:s',24554457865);
?>
Demo5.php
<?php
//getdate()
也可以轉換時間戳
//print_r(getdate());
//Array
(
[seconds]
=>
26
[minutes]
=>
34
[hours]
=>
10
[mday]
=>
20
[wday]
=>
1
[mon]
=>
4
//[year]
=>
2015
[yday]
=>
109
[weekday]
=>
Monday
[month]
=>
April
[0]
=>
1429526066
)
$t
=
getdate();
echo
$t['year'];
//傳遞一個時間戳
print_r(getdate(1029526066));
?>
Demo6.php
<?php
//直接獲取當前時間戳
//echo
time();//1429526328
//這個
time()
可以調整時間
//大家可以發現
time()
很有用處,可以過去現在和將來
echo
date('Y-m-d
H:i:s',time()+60*60*8);
?>
Demo7.php
<?php
//獲取特定指定時間的時間戳
//這是當前的時間戳
//echo
time();
//我要取得
2008-08-08
08:08:08
$beijing2008
=
mktime(8,8,8,8,8,2008);
echo
date('Y-m-d
H:i:s',$beijing2008);
?>
Demo8.php
<?php
//使用時間戳計算時間差
$now
=
time();//當前的時間戳
$wnow
=
mktime(0,0,0,8,16,2016);
//兩個時間戳相減可以得到差秒
echo
round(($wnow
-
$now)/60/60,2).'相差這幾個小時';
?>
Demo9.php
<?php
//將人可讀的時間,字元串形式,轉換成時間戳
$a
=
strtotime('2010-7-16
15:15:15')-strtotime('2010-7-16
15:14:15');
if($a
>=
60){
echo
'請這位先生休息一會。';
}else{
echo
$a;
}
?>
Demo10.php
<?php
//獲取當前文件的修改時間戳
echo
date('Y-m-d
H:i:s',getlastmod());
?>
Demo11.php
<?php
//配置系統環境變數
echo
date('Y-m-d
H:i:s');
echo
'<br/>';
//我開始設置時區
putenv('Tz=Asia/Shanghai');
echo
date('Y-m-d
H:i:s');
?>
Demo12.php
<?php
//putenv('Tz=Asia/Shanghai');
//獲取當前時區
echo
date_default_timezone_get();
echo
'<br/>';
//開始配置默認時區
date_default_timezone_set('Asia/Shanghai');
echo
date('Y-m-d
H:i:s')
;
echo
'<br/>';
echo
date_default_timezone_get();
?>
Demo13.php
<?php
date_default_timezone_set('Asia/Shanghai');
//"tm_sec"
-
秒數
//"tm_min"
-
分鍾數
//"tm_hour"
-
小時
//"tm_mday"
-
月份中的第幾日
//"tm_mon"
-
年份中的第幾個月,從
0
開始表示一月
//"tm_year"
-
年份,從
1900
開始
//"tm_wday"
-
星期中的第幾天
//"tm_yday"
-
一年中的第幾天
//"tm_isdst"
-
夏令時當前是否生效
print_r(localtime(time(),true));
//Array
(
[tm_sec]
=>
37
[tm_min]
=>
15
[tm_hour]
=>
19
//[tm_mday]
=>
20
[tm_mon]
=>
3
[tm_year]
=>
115
//[tm_wday]
=>
1
[tm_yday]
=>
109
[tm_isdst]
=>
0
)
?>
Demo14.php
<?php
//返回時間戳和微秒數
//怎麼計算頁面運行載入時間
//頁面打開的時候獲取一個時間
//頁面結束的時候獲取一個時間
//用結束的時間減去打開的時間,那麼就是運行時間
//
list($a,$b)=explode('
',microtime());
//
echo
$b;
function
fn(){
list($a,$b)=explode('
',microtime());
return
$a+$b;
//返回出精確的秒數
}
//在頁面打開的時候,獲取一個時間
$start_time
=
fn();
for($i=0;$i<10000000;$i++){
//
}
//頁面結束的時候,獲取一個時間
$end_time
=
fn();
echo
round(($end_time
-
$start_time),4);
?>
更多關於PHP相關內容感興趣的讀者可查看本站專題:《php日期與時間用法總結》、《PHP數組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php面向對象程序設計入門教程》、《PHP網路編程技巧總結》、《php字元串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。

❹ java中如何將任何形式的String轉成Date

我曾經寫過一個類,專門用作數字時間的轉化,現在發給你

package cn.siyu.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StringToDateCoversion {
private static List<String> list = new ArrayList<String>();

/*public void setList(List<String> list) {
this.list = list;
}*/
private List<String> getList() {
return list;
}
private StringToDateCoversion(List<String> list) {
this.list=list;
}

public StringToDateCoversion(String list) {
this.list.add(list);
}
public String DateToStringFormat() throws Exception{
SimpleDateFormat format= new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
return format.format(getDate());
}

/**
*
* the date as follows can conversion(all the character be transform,even mixed-use except the last one):<br>
* 2012-10-10 05-30-30 12-10-10 05-30-30<br>
* 2012/10/10 05/30/30 12/10/10 05/30/30<br>
* 2012|10|10 05|30|30 12|10|10 05|30|30<br>
* 2012.10.10 05.30.30 12.10.10 05.30.30<br>
* 2012 10 10 05 30 30 12 10 10 05 30 30<br>
* 2012,10,10,05,30,30 12,10,10,05,30,30<br>
* 20121010053030 121010053030 <br>
*
* so as can contains the hours ,minutes,second<br>
*
* try to format the string to data,<br>
* if the year>1000,will as it.example:1870-10-10 --->1870-10-10<br>
* if 65 < year < 100,the year will be add "19". example:70-10-10 --->1970-10-10<br>
* if 00 < year < 40,the year will be add "20" . example:10-10-10 --->2010-10-10<br>
* @throws ParseException
* */
public Date getDate() throws ParseException{
//StringToDateCoversion date = new StringToDateCoversion("2012,10/10");
StringToDateCoversion splitList = this.splitList("-").splitList("/").splitList(" ").splitList(",").splitList("\\.").splitList("\\|");
List<String> date = splitList.getList();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
String date1=null;
Date retuendate=null;
/**
* deal with 20121010 121010
* */
if(date.size()>1){
date1 = ListtoString(date);
retuendate = format.parse(date1);
}
if(date.size()==1){
/**
* if the string not eval ,should add zero to the first position
* */
if(date.get(0).length()%2==1){
date.set(0, "0"+date.get(0));
}
date1 = ListtoString(getList(subList(date, 2)));
retuendate = format.parse(date1);
}

return retuendate;
}
private List<String> subList(List<String> date,int i){
List<String> returnDate = new ArrayList<String>();
String string = date.get(0);
returnDate.add(string.substring(0, i));
for(;i<string.length();i=i+2){
returnDate.add(string.substring(i, i+2));
}
return returnDate;
}
private List<String> getList(List<String> date){
int size = date.size();
Integer first =Integer.parseInt(date.get(0));
Integer second =Integer.parseInt(date.get(1));
if(second>12||size==4||size==7){
for (int i = 0; i < size-1; i++) {
String infor=i==0?date.get(i):"";
date.set(i,(infor+date.get(i+1)));
}
date.remove(size-1);
return date;
}else if(first<=40){
date.set(0, "20"+date.get(0));
}else if(first>40){
date.set(0, "19"+date.get(0));
}
return date;
}
private String ListtoString(List<String> date){
String returnDate=null;

Integer first = Integer.parseInt(date.get(0));
if(first>100){
returnDate=first.toString();
}else if(first<=50&&first>=0){
returnDate="20"+first.toString();
}else if(first<100&&first>50){
returnDate="19"+first.toString();
}
for(int i=1;i<6;i++){
if(date.size()<=i){
returnDate=returnDate+"-00";
}else{
returnDate=returnDate+"-"+date.get(i);
}
}
return returnDate;
}

private StringToDateCoversion splitList(String regex){
if(list==null®ex==null){ // 這個地方的代碼看好,是if(list==null & regex ==null),提交的時候總是被轉義,你自己修改一下

return null;
}
List<String> returnlist = new ArrayList<String>();
Object[] array = list.toArray();
for (Object Obj : array) {
String[] splitList = Obj.toString().split(regex);
for (String string : splitList) {
returnlist.add(string);
}
}
return new StringToDateCoversion(returnlist);
}
}

❺ php 怎麼將字元轉成數字

第一種轉換方式: 強制轉換;

代碼:

(5)phpstringtodate擴展閱讀:

PHP的數據類型轉換屬於強制轉換,允許轉換的PHP數據類型有:

(int)、(integer):轉換成整形;

(float)、(double)、(real):轉換成浮點型;

(string):轉換成字元串;

(bool)、(boolean):轉換成布爾類型;

(array):轉換成數組;

(object):轉換成對象。

❻ 用PHP怎麼取得7天前的日期

$date = date('Y-m-d', strtotime('-7 days')); //保留年-月-日

<?php echo echo date(」Y-m-d H:i:s」,strtotime(」-7 day」)) ;?> //保留年-月-日 時:分:秒

strtotime('-7 days') 獲得的是時間戳

strtotime('now')); //獲取當前的時間戳

time() //獲取的時間戳

(6)phpstringtodate擴展閱讀:

一、使用函式 date() 實現

在編輯器中輸入<?php echo $showtime=date("Y-m-d H:i:s");?>,點擊回車就可以得知當前的時間。其中Y是代表4位的年份,H是24小時制,i 是分鍾,如: "00" 至 "59" 。s -是秒,如: "00" 至 "59" 。

d 是幾日,二位數字,若不足二位則前面補零。 如: "01" 至 "31" 。m代表月份,二位數字,若不足二位則在前面補零,如: "01" 至 "12" 。

二、使用time函數

在編輯器中輸入echo date("y-m-d",$time)點擊回車就可以得知當前的時間,其中Y是代表4位的年份,m代表月份,二位數字,若不足二位則在前面補零,如: "01" 至 "12" 。d 是幾日,二位數字,若不足二位則前面補零。 如: "01" 至 "31" 。

三、使用strftime函數

在編輯器中輸入echo strftime ("%hh%m %a %d %b" ,time());點擊回車就可以得知當前的時間。

❼ java中String轉換成Date

給你個我自己寫的工具類 是互轉的 什麼類型的都可以 package com.constants;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;public class StrinAndDate {
/**
* @author angus
* String和DATE類型相互轉化
*/
/*
* DateToString(Date date),時間轉換成字元串
*/
public static String DateToString(Date date) {
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
String s = formatDate.format(date);
try {
date = formatDate.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return formatDate.format(date);
}
/*
* Date StringToDate(String s),字元串轉換成時間
*/
public static java.util.Date StringToUtilDate(String s) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
try {
Date date = sdf.parse(s);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}

public static java.sql.Date StringToSqlDate(String s) {
try{
return java.sql.Date.valueOf(s);
}catch(Exception e){
e.printStackTrace();
return null;
}
}

}

熱點內容
linux64位內存 發布:2025-07-16 01:02:36 瀏覽:959
壓縮衣尺碼表 發布:2025-07-16 00:47:33 瀏覽:639
安卓恢復了出廠怎麼找回照片 發布:2025-07-16 00:43:56 瀏覽:932
為什麼說伺服器已停止響應 發布:2025-07-16 00:29:36 瀏覽:391
python判斷字元串是否為空 發布:2025-07-16 00:21:47 瀏覽:210
安卓轉蘋果用什麼軟體 發布:2025-07-16 00:21:45 瀏覽:628
安卓官服如何登ios 發布:2025-07-16 00:21:32 瀏覽:624
天龍搶店腳本 發布:2025-07-16 00:14:47 瀏覽:958
華為榮耀存儲卡 發布:2025-07-16 00:10:40 瀏覽:659
mysql創建utf8資料庫 發布:2025-07-16 00:04:08 瀏覽:105