In order to get the date in the right format I want I used date("d-m-Y"). Now I want to get the time in addition to the date in the following format H:M:S How can I procede ?
Anytime you have a question about a particular function in PHP, the easiest way to get quick answers is by visiting php.net, which has great documentation on all of the language's capabilities.
Looking up a function is easy, just visit http://php.net/<function name> and it will forward you to the appropriate place. For the date function, we'll visit http://php.net/date.
We immediately learn a couple things about this function by examining its signature:
string date ( string $format [, int $timestamp = time() ] )
First, it returns a string. That's what the first string in the above code means. Secondly, the first parameter is expected to be a string containing the format. There is an optional second parameter for passing in your own timestamp (to construct strings from some time other than now).
date("d-m-Y") // produces something like 03-12-2012
In this code, d represents the day of the month (with a leading 0 is necessary). m represents the month, again with a leading zero if necessary. And Y represents the full 4-digit year. All of these are documented in the aforementioned link.
To satisfy your request of getting the hours, minutes, and seconds, we need to give a quick look at the documentation to see which characters represents those particular units of time. When we do that, we find the following:
h 12-hour format of an hour with leading zeros 01 through 12
i Minutes with leading zeros 00 to 59
s Seconds, with leading zeros 00 through 59
With this in mind, we can no create a new format string:
date("d-m-Y h:i:s"); // produces something like 03-12-2012 03:29:13
Hope this is helpful, and I hope you find the documentation has benefiting to your development as I have to mine.
You can combine both in the same date function call
date("d-m-Y H:i:s");
You can have both formats as an argument to the function date():
date("d-m-Y H:i:s")
Check the manual for more info : http://php.net/manual/en/function.date.php
As pointed out by #ThomasVdBerge to display minutes you need the 'i' character
Related
I need to set variable, let's call it $times to specific amount of 8AM. I tried using mktime(08,00) but it returns
Parse error: Invalid numeric literal
Since I'm new to php, I still don't know which function is best used for thing such as this, weather it is time() date() or so.
My question is: how do I set $times to be 8AM of current day?
I've checked a lot of similar questions, but none of them have an answer. Not even the one that this is marked duplicate of.
Just remove the leading zeros.
mktime(8, 0);
It's because PHP is interpreting 08 as an octal number, and 8 is out of range in octal (0-7).
You can use date()
Dateformat reference: https://www.php.net/manual/en/datetime.format.php
$time = date("H:i") // 08:00
php date("d-m-Y") code show 11-09-2017. I change it to string and need to compare it to other string, 11-9-2017. How can I change the php date("") to return a result with no 0.
for example change:
08-09-2017 -> 8-9-2017
Thank for the answer.
You used the wrong flags to format the date, you need j and n to meet your criteria as per the documentation.
j Day of the month without leading zeros 1 to 31
n Numeric representation of a month, without leading zeros 1 through 12
Therefore your code becomes,
date("j-n-Y");
You can use n in place of m date("d-n-Y");
date("j-n-Y"); to have it without leading zeros day/month
User strtotime() function convert dates to time and then compare this two dates. date("j-n-Y"); will also work to get exact date.
I have checked out the following question/responses:
How do I get the format of “yyyy-MM-ddTHH:mm:ss.fffZ” in php?
The responses include links to Microsoft documentation to format dates but these do not work in PHP.
The the top answer suggest
date('Y-m-dTH:i:s.uZ') //for the current time
This outputs
2013-03-22EDT12:56:35.000000-1440016
Background
I am working with an API which requires a timestamp in the format above. The API is based in the UK (GMT) and my server is in Australia (AEST).
The example given in the API documentation ask for the date to be in this format:
2011-07-15T16:10:45.555Z
The closest I can get to this is date('c') which outputs:
2014-07-03T16:41:59+10:00//Notice the Z is replaced with a time diff in hours
I believe the 'Z' refers to a Zone but it is not mentioned in the PHP documentation.
Unfortunatly when I post this format, the API is reading the time and taking 10 hours off. I get an error saying that the date cannot be in the past (as it is checking against the local time in Melbourne, but seeing a time 10 hours earlier).
I have tried trimming the timestamp to remove the +1000 which the API accepts, but the record is showing as created as 10 hours earlier.
I need to match the timestamp required but I cannot find any way to replicate the above output, in PHP for Melbourne, Australia. Any assistance is much appreciated.
First question on SO so please let me know how I have gone
Z stands for the timezone UTC and is defined in ISO-8601, which is your desired output format, extended by the millisecond part.
Before outputting the time, you'll need to transfer local times to UTC:
$dt = new DateTime();
$dt->setTimeZone(new DateTimeZone('UTC'));
then you can use the following format string:
echo $d->format('Y-m-d\TH-i-s.\0\0\0\Z');
Note that I've zeroed the millisecond part and escaped the special characters T and Z in the format pattern.
The 3 number last before Z is just the 3 decimal place of time in milliseconds
The function microtime(true) gave the current time in milliseconds, would output like 1631882476.298437
In this situation it would be .298Z
Examples
1652030212.6311 = .631Z
1652030348.0262 = .026Z
1652030378.5458 = .545Z
Codes
$milliseconds = microtime(true);
// Round to integer
$timestamp = floor($milliseconds);
// Get number after dots
$uuuu = preg_replace("/\d+\./", "", "$milliseconds");
// Get last 3 number decimal place
$u = substr($uuuu, 0, 3);
// Print date by the timestamp timestamp
echo date("Y-m-d\TH:i:s", $timestamp). ".{$u}Z";
I am new to PHP and I am trying to learn more of php date and time but I seem to get stuck with this.
I have this date format:
ddMMyyHHmmss
And an example is 120813125055 but I am trying to manipulate the string such that it will give me the format of:
yyyy-MM-dd HH:mm:ss (in the example above, 2013-08-12 12:50:55)
I tried to do something like:
date('Y-m-d H:i:s', strtotime('120813125055'));
But it always gives me a result of 1969-12-31 18:00:00.
I assume that I need to do some string manipulation in PHP for this but I was wondering if there is an easier and more efficient way to do it?
I think what you're looking for is in the second response answered here: how to re-format datetime string in php?
To summarize (and apply to your example), you could modify the code like this.
$datetime = "120813125055";
$d = DateTime::createFromFormat("dmyHis", $datetime);
echo $d->format("Y-m-d H:i:s");
Use date_create_from_format:
$ts = date_create_from_format('dmyHis', '120813125055');
$str = date('Y-m-d H:i:s', $ts);
strtotime() only works on EASILY recognizable formats. Your is a ugly mix of garbage, so no surprise that strtotime bails with a boolean FALSE for failure, which then gets typecast to an int 0 when you tried feed it back into date().
And of course, note that your time string is NOT y2k compliant. two digit years should never ever be used anymore, except for display purposes.
You're using your function call and the argument the wrong way around.
In your example, php will try to return you the date for which the time is 'strtotime('120813125055')', and this function returns false (interpreted as 0). So you get returned the date formatted in 'Y-m-d H:i:s' for the Unix epoch.
You will need to get the actual timestamp of your string, so use http://www.php.net/manual/en/datetime.createfromformat.php.
You are mistaken here..
I tried to do something like:
date('Y-m-d H:i:s', strtotime('120813125055'));
You shouldn't use only numbers ( doesnt matter its an integer or a string ), than it will always give you the same thing.
You can use any other valid date and time ( E.G. 6 Jun 2013, 5 may 12...) . Because what strtotime() do is detect a valid date and convert it into timestamp.
I have PHP times for the start and end times of an event. This is a simple <?php time(); ?> for some future date's start and end time. I was wondering if anyone knew of a way to take either the numerical format (PHP time()) or taking some string value representing that time (I could do a strtotime($sometimevalue);) and turn it into the required Google Calendar time format.
Enough talking - here is an example of the time format:
20150107T003000Z/20150107T023000Z
This equates to January 6th, 2015 at 5:30 PM to January 6th, 2015 at 7:30PM.
So can someone explain to me how to translate a time() to this format?
Try this:
date_default_timezone_set('UTC');
$date = date("Ymd\THis\Z");
The first line sets the default timezone to use to be UTC (this is the "Z" at the end of the formatted time: Z = "Zulu Time"). I did this since I don't know if Google is expecting a UTC time or not. If you can use other timezones, then you can use one of the other timezone formats available.
In the next line, I use date to format the current Unix timestamp (when no timestamp is passed to date it defaults to the current time - i.e. time()). I'll break it apart for you:
Y - The four-digit year
m - The two-digit (including leading zero, if necessary) month
d - The two-digit (including leading zero, if necessary) day of the month
\T - The literal character T, which is a delimiter identifying that the time portion of the date is beginning. The slash is to escape the T, as it is otherwise used to display the timezone abbreviation (e.g. "PST")
H - The two-digit (including leading zero, if necessary) hour
i - The two-digit (including leading zero, if necessary) minute
s - The two-digit (including leading zero, if necessary) second
\Z - The literal character Z, indicating zulu time as discussed above. The slash is to escape the T, as it is otherwise used to display the timezone in seconds from UTC.
For reference, and to be sure I interpreted the question accurately, this code:
date_default_timezone_set('UTC');
echo date("Ymd\THis\Z", time());
Currently displays this result:
20110415T014623Z
I should note that you could also use gmdate() in place of date() and eliminate the need for the date_default_timezone_set() call, since gmdate() returns the result in GMT. I only hesitate to mention this because I've never been 100% clear on the difference, if any, between GMT and UTC, especially with other timezones/periods like BST (British Summer Time) and how they alter GMT, if at all. If someone could clarify this in the comments, I would be most appreciative.
Working solution taken from http://php.net/manual/en/function.date.php
Credit goes to Boris Korobkov.
// boris at psyonline dot ru 14-Jun-2007 03:05
<?php
/**
* Get date in RFC3339
* For example used in XML/Atom
*
* #param integer $timestamp
* #return string date in RFC3339
* #author Boris Korobkov
*/
function date3339($timestamp=0) {
if (!$timestamp) {
$timestamp = time();
}
$date = date('Y-m-d\TH:i:s', $timestamp);
$matches = array();
if (preg_match('/^([\-+])(\d{2})(\d{2})$/', date('O', $timestamp), $matches)) {
$date .= $matches[1].$matches[2].':'.$matches[3];
} else {
$date .= 'Z';
}
return $date;
}
?>
Background:
From
http://www.ibm.com/developerworks/opensource/library/os-php-xpath/
I saw
"The published and updated elements use the RFC 3339 time-stamp format. "
And figured I ought google "rfc3339 PHP" to find a function that implements this format
It's little bit old but Google expects to get time in a ISO 8601. Example :
$objDateTime = new DateTime('NOW');
$isoDate = $objDateTime->format(DateTime::ISO8601);
SOURCE
The easiest way I use:
$postBody = new Google_Service_Calendar_Event(array(
.......
'start' => array(
'dateTime' => date_format($event_Start_datetime,'c');
........
//'c' - formats DateTime object as needed for Google Calendar