茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1670944 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_DPP_0066.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

PHP特區 : [轉貼]PHP日期的加減法

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]PHP日期的加減法
【教學】PHP日期的加減法

顯示格式2004-01-08

在2004-01-08再加上15天 = 2004-01-23
$years = date("Y"); //用date()函式取得目前年份格式0000
$months = date("m"); //用date()函式取得目前月份格式00
$days = date("d"); //用date()函式取得目前日期格式00
$day = date("Y-m-d",mktime(0,0,0,$months,$days+15,$years));
echo $day;

在2004-01-08再加上6個月 = 2004-07-08
$years = date("Y"); //用date()函式取得目前年份格式0000
$months = date("m"); //用date()函式取得目前月份格式00
$days = date("d"); //用date()函式取得目前日期格式00
$day = date("Y-m-d",mktime(0,0,0,$months+6,$days,$years));
echo $day;

在2004-01-08再加上2年 = 2006-01-08
$years = date("Y"); //用date()函式取得目前年份格式0000
$months = date("m"); //用date()函式取得目前月份格式00
$days = date("d"); //用date()函式取得目前日期格式00
$day = date("Y-m-d",mktime(0,0,0,$months,$days,$years+2));
echo $day;

在2004-01-08再減15天 = 2003-12-24
$years = date("Y"); //用date()函式取得目前年份格式0000
$months = date("m"); //用date()函式取得目前月份格式00
$days = date("d"); //用date()函式取得目前日期格式00
$day = date("Y-m-d",mktime(0,0,0,$months,$days-15,$years));
echo $day;

在2004-01-08再減6個月 = 2003-07-08
$years = date("Y"); //用date()函式取得目前年份格式0000
$months = date("m"); //用date()函式取得目前月份格式00
$days = date("d"); //用date()函式取得目前日期格式00
$day = date("Y-m-d",mktime(0,0,0,$months-6,$days,$years));
echo $day;

在2004-01-08再減2年 = 2002-01-08
$years = date("Y"); //用date()函式取得目前年份格式0000
$months = date("m"); //用date()函式取得目前月份格式00
$days = date("d"); //用date()函式取得目前日期格式00
$day = date("Y-m-d",mktime(0,0,0,$months,$days,$years-2));
echo $day;



原文出處:∮Ω奧米加空間∮-【教學】PHP日期的加減法
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]取得一個月的天數/月曆模組

[php] 取得一個月的天數/月曆模組

最近有些 case 需求有用到製作月曆的 function
像是民宿/理髮店/牙醫 等等 需要預約的行業
都會需要製作日曆的畫面 或是管理介面

之前有用過 CodeIgniter 的 Calendar Library
不錯用

他會幫你畫出月曆
但是格子的內容無法自定
有時需要塞一些其他文字/按鈕
或是想在按按鈕的時候觸發一些 js
就覺得有點缺陷 想自己做月曆的模組

在規劃這個模組的時候
遇到的第一個問題是
需要知道每個月有幾天
然後寫一個 for-loop
把每天的日期 星期 放到一個 array 裡面
再來做其他的動作

找了一下相關的資料
發現 mktime 可以做類似的動作


$timestamp = mktime(0,0,0,12,0,2011);



這樣表面上可以取得 2011年12月0日 的 timestamp
但是12月沒有0日
所以會抓到 11月的最後一天 也就是30日的 timestamp

透過這個特別的動作
就可以取得某個月最後一天的日期(天數)



$begin = mktime(0,0,0,$month,1,$year);
$end = mktime(0,0,0,$month+1,0,$year);
$diff = $end - $begin;
$days = ($diff /86400)+1;


這樣的話就可以取得天數

原文出處:路破皮的部落格: [php] 取得一個月的天數/月曆模組
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]如何用PHP取得上個月的最後一天?
如何用PHP取得上個月的最後一天?
$temp = strtotime("-1 month", time());
echo "上月最後一天是".date("Y-m-t", $temp);

其他依此類推..


原文出處:小魚兒筆記本: 如何用PHP取得上個月的最後一天?
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Converting string to MySQL timestamp format in php
Converting string to MySQL timestamp format in php

I am writing a query in php using a string sent from a android java application.
The query is something like :
$insertSQL = sprintf("INSERT INTO app_DuckTag (taste) VALUES (%s) WHERE species=%s AND timestamp=%s",
                         GetSQLValueString($_POST['taste'], "text"),
                         GetSQLValueString($_POST['species'], "text"),
                         GetSQLValueString($_POST['timestamp'], "text"));

But I doubt timestamp is stored as a string inside MySQL.
How should I convert the string to time format as in MySQL?
The strtotime(string) php function converts it to unix time.
But the MySQL stores it differently, I guess. Thank you.

EDIT: This is how it shows up in MySQL phpmyadmin: 2011-08-16 17:10:45

EDIT: My query is wrong though. Cannon use a where clause with Insert into.
The query has to be UPDATE .... SET ... = ... WHERE ....
But the accepted answer is the correct way to use the time inside the WHERE clause.
php android mysql string timestamp
shareimprove this question



This should be all:
date("Y-m-d H:i:s", strtotime($_POST['timestamp']));

if you want to check for timezones and such you should use strftime instead of date
shareimprove this answer



If you can get the input 'string' (which you haven't provided the format that you're receiving it in) to a Unix timestamp value, you can easily convert it to a MySQL datetime format like this:
$mysqldatetime = date("Y-m-d H:i:s", $unixTimeStampValue);

This will produce a string similar to the following:
    2011-08-18 16:31:32

Which is the format of the MySQL datetime format. If you are using a different time format, then the first argument to the date function will be different.

See the manual for the date function for more information and other ways you can format the value that it returns.

Edit

You are receiving a string formatted in MySQL datetime format. After sanitizing it, you can insert it directly into the database. To MySQL it may be a 'datetime' data type, but to PHP it is simply a string (just like your entire SQL query is nothing more than a string to PHP).
$timestamp = mysql_real_escape_string($_POST['timestamp']);

You should be able to safely insert that into your database for the timestamp field.


原文出處:
android - Converting string to MySQL timestamp format in php - Stack Overflow
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|