php日期输出yy而不是yyyy

时间:2015-08-07 09:36:03

标签: php

我对PHP知之甚少。

我试图以dd / mm / yyyy格式显示或传递月份的第一天,但​​结果是dd / mm / yy。

我的PHP查询是

<? php
$month='Aug'
$year='2015'

$c=pdgwoconnect(); 
$sqlmy="SELECT TO_DATE("$month/$year","MM/YYYY")FRMDT, TO_DATE("$nextm/$year","MM/YYYY")TODT FROM DUAL";
$resultmy = OCIParse($c, $sqlmy);
OCIExecute($resultmy);

while ($row = oci_fetch_array($resultmy, OCI_ASSOC)) {
    $frmdt= $row["FRMDT"];
    $todt= $row["TODT"];
    echo $frmdt;
    echo "</br>";
    echo $todt;
}
?>

结果是01-AUG-15和01-SEP-15

我希望这个格式为01-08-2015或01-AUG-2015,就像这样。

3 个答案:

答案 0 :(得分:1)

首先:为什么需要从数据库中选择日期?

您的数据库引擎/数据库连接上设置的时区可能与您想要的时区不同,并且它消耗资源(主要是时间)。所以使用DB来获取日期和时间似乎是不必要的,未经修改的也是无效的

除非你有非常严格的理由,否则我建议使用PHP来检索你需要的日期,以下是你如何做到的:

<?php
   // your input data as posted
   $month='Aug';
   $year='2015';
   $day = '01'; // since you need a first day of the month

   //create proper timezone that you need - I chose "Europe/London"
   $timeZoneString = 'Europe/London';
   $timeZone = new DateTimeZone($timeZoneString);

   //date string has to match format given in DateTime::createFromFormat()
   $dateString = "{$year}-{$month}-{$day}";
   $dateTime = DateTime::createFromFormat('Y-M-d', $dateString, $timeZone);

   //you could see here what DateTime object looks like
   //var_dump($dateTime);

   //print the current date - the 1st Aug 2015 in deisred format
   echo $dateTime->format("d/m/Y"); // prints "01/08/2015"

   //create proper datetime interval
   $dateIntervalString = 'P1M'; // +1 month
   $dateInterval = new DateInterval($dateIntervalString);

   //add datetime interval to your date
   $dateTime->add($dateInterval);

   //print the date as you like, e.g. "dd/mm/yyyy"
   echo $dateTime->format("d/m/Y"); // prints "01/09/2015"

我检查了这段代码并且它有效,并且它比您的方法具有以下优势:

  • 它没有连接到DB(耗费时间和资源)
  • 时区灵活(见valid timezones list
  • 您可以随时更改生成的格式

顺便说一句:我强烈建议您阅读this post on SO regarding date, time databases related best practises

答案 1 :(得分:0)

你可以用  如果你想像2015年一样,而不是在日期格式中使用'Y'

<?php
 $date = date('d-m-Y');
echo $date
?>

答案 2 :(得分:0)

您可以使用php函数#include "stdafx.h" #include <metahost.h> #include <mscoree.h> #include <assert.h> //#include <Windows.h> //#using <WpfWithoutService.dll> //using namespace System; //using namespace System::Threading; //using namespace WpfWithoutService; #pragma comment(lib, "mscoree.lib") /*ref class MainWinClass { public: void MainForm() { MainWindow^ mainwin = gcnew MainWindow(); //mainwin->Activate(); //mainwin->InitializeComponent(); } };*/ int _tmain(int argc, _TCHAR* argv[]) { HRESULT hr; //BOOL fLoadable; ICLRMetaHost *pMetaHost = NULL; LPCWSTR pwzVersion = (LPCWSTR)"v4.0.30319"; //hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost); hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_PPV_ARGS(&pMetaHost)); if (FAILED(hr)) { wprintf(L"CLRCreateInstance failed w/hr 0x%08lx\n", hr); } ICLRRuntimeInfo *pRuntimeInfo = NULL; // Get the ICLRRuntimeInfo corresponding to a particular CLR version. //hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID *)&pRuntimeInfo); hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&pRuntimeInfo)); if (FAILED(hr)) { wprintf(L"ICLRMetaHost::GetRuntime failed w/hr 0x%08lx\n", hr); //goto Cleanup; } /*Check if the specified runtime can be loaded into the process. This // method will take into account other runtimes that may already be // loaded into the process and set pbLoadable to TRUE if this runtime can // be loaded in an in-process side-by-side fashion. hr = pRuntimeInfo->IsLoadable(&fLoadable); if (FAILED(hr)) { wprintf(L"ICLRRuntimeInfo::IsLoadable failed w/hr 0x%08lx\n", hr); //goto Cleanup; } if (!fLoadable) { wprintf(L".NET runtime %s cannot be loaded\n", "4.0.30319.18063"); //goto Cleanup; }*/ // Load the CLR into the current process and return a runtime interface pointer. ICLRRuntimeHost *pClrRuntimeHost = NULL; hr = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&pClrRuntimeHost)); if (FAILED(hr)) { wprintf(L"ICLRRuntimeInfo::GetInterface failed w/hr 0x%08lx\n", hr); } // Start the CLR. if (hr == S_OK) { hr = pClrRuntimeHost->Start(); if (FAILED(hr)) { wprintf(L"CLR failed to start w/hr 0x%08lx\n", hr); //goto Cleanup; } } /*Create a instance of the class declared in c# dll //MainWindow^ mainwin = gcnew MainWindow(); //mainwin->SetupLogFiles("iptcom_debug_WpfWithoutService.txt"); MainWinClass^ win = gcnew MainWinClass; ThreadStart^ threadDelegate = gcnew ThreadStart(win, &MainWinClass::MainForm); Thread^ newThread = gcnew Thread(threadDelegate, 0); newThread->SetApartmentState(ApartmentState::STA); newThread->Start();*/ //Load an assembly and call the required function if (hr == S_OK) // if CLR is started successfully { DWORD dwRet; hr = pClrRuntimeHost->ExecuteInDefaultAppDomain(L"D:\\IPTCom\\IPTComServiceTest_Canada\\IPTComServiceTests_rev2\\Unmanaged_App\\ConsoleApplication.exe", L"ConsoleApplication.Program", L"Main",L"", &dwRet); if (FAILED(hr)) { wprintf(L"ExecuteInDefaultAppDomain failed to start w/hr 0x%08lx\n", hr); } } if (pMetaHost) { pMetaHost->Release(); pMetaHost = NULL; } if (pRuntimeInfo) { pRuntimeInfo->Release(); pRuntimeInfo = NULL; } if (pClrRuntimeHost) { // Please note that after a call to Stop, the CLR cannot be // reinitialized into the same process. This step is usually not // necessary. You can leave the .NET runtime loaded in your process. //wprintf(L"Stop the .NET runtime\n"); //if (pClrRuntimeHost->Stop() == S_OK) if (1) { pClrRuntimeHost->Release(); pClrRuntimeHost = NULL; } else { pClrRuntimeHost->Release(); pClrRuntimeHost = NULL; } } return 0; }

strtotime

试试这个希望它会起作用