After a long time I needed to use date function of PHP. I wrote something like:
echo date('now');
and I got the output below:
1220123
What does that mean ?
From the PHP manual :
n Numeric representation of a month, without leading zeros
o ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that
year is used instead. (added in PHP 5.1.0)
w Numeric representation of the day of the week
So, date("now") displays 12 (n), 2012 (o) and 3 (w).
You're probably looking for :
date("Y-m-d") for a date
date("Y-m-d H:i:s") for a datetime
"now" is not a valid parameter for for this expectation, infact it should be strtotime function here, not date.
Date considers your now as
n
Numeric representation of a month, without leading zeros
o
ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
w
Numeric representation of the day of the week
you need to give a valid format to date function (not recognize the 'now' string as meaning of now )
$date = date("Y-m-d H:i:s");
or you can use the DateTime class
$date = new DateTime();
Seems you consider "now" as a word to get the current date and time, however it would compile on each character. Here is the explanation how it'll compile.
n = Month in number
o = It considers as a year in ISO-8601.
w = Week in number
So that's why it's returning you the date, year and number of week in a month.
Hope I can explain you bit easily.
"now" is not a valid parameter for date()
Correct syntax to print current date in
yyyy-mm-dd hours minutes seconds
format is as given below
echo date('Y-m-d h:i:s');
also see PHP manual for details of date() function
http://php.net/manual/en/function.date.php
Related
Given an invalid date in the format MMDD, say 1332, the DateTime::createFromFormat method in PHP 5.3.0+ will accept it as a valid date, 0201 in that same MMDD format.
Code snippet:
$dateobj = DateTime::createFromFormat("md", "1332");
if ($dateobj) {
print $dateobj->format('Y/m/d H:i:s') . "\n";
}
Output:
2019/02/01 20:59:37
Obviously, 13 is not a valid month of the year and 32 is not a valid day of any month. It's also apparent that DateTime::createFromFormat is "rolling over" those numbers, as if it were adding 13 months and 32 days to a zero value (the current datetime's year). One month after December (month 12) is January (in this case month "13"), and 32 days after January 1 (inclusive) is February 1.
Is there a way to still use DateTime::createFromFormat but disable, override, or otherwise work around that specific over-permissive behavior?
One way to use DateTime::createFromFormat, and it alone, is to compare the DateTime object it created to the original input:
$dateobj = DateTime::createFromFormat($format, $date);
if ($dateobj && $dateobj->format($format) == $date) {
print($dateobj->format($format));
}
If they're the same, the input date is valid. If they're not the same, DateTime::createFromFormat did its "rollover" calculation and you have an invalid input date.
This was actually a solution given on the checkdate PHP Manual page as a User Contributed Note, though it doesn't involve checkdate itself.
I have no idea about output of this function.
" strtotime(date("j"),date("F"),date("t")) "
Output: 1475734243
That output is changing according to the time.
my code block is:
<?php
$day= date("j");
$month= date("F");
$year= date("Y");
//calendar Variables
$currentTimeStamp= strtotime($day-$month-$year);
echo $currentTimeStamp;
?>
date("j") gives Day of the month without leading zeros (1 to 31).
date("F") gives A full textual representation of a month, such as January or March.
date("Y") gives A full numeric representation of a year, 4 digits (eg: 2016).
I am not sure what part is confusing but if I got it right that you want to display the current date with that format then you should use this without having to use strtotime()
echo date("j F Y");
Sample working output:
https://repl.it/Dodz/0
The output is a unix time stamp.
echo $day.'-'.$month.'-'.$year;
echo date('j-F-Y', strtotime($currentTimeStamp));
This will give you the answer you are looking for.
See strtotime documentation.
It returns the number of seconds passed since the U-day (January 1 1970 00:00:00 UTC) to the date (in string representation) that is provided as an argument. It's also called "Unix timestamp" and "Unix epoch (time)"
My date formatted like this "2000-5-1". First digit represents 4 digit year. The second is number of week in year, and the last one represents number of day in week.
No matter what I do, function always returns false, my code is following:
date_create_from_format("Y-W-N", "2000-5-1")
(docs)
Please avoid solutions that are using magic words like "+1 day" etc.
Creating a DateTime object from a compound format with year/week/day is described in the compound formats section of the supported date and time formats section of the PHP Docs:
$x = new DateTime("2000-W05-1");
var_dump($x);
Note that the week number requires a leading zero
Result is a DateTime object for 2000-01-31
If you look at the documantation of DateTime::createFromFormat (which is what you are using with an alias) and date(), there is not a 100% overlap of the formats, so you have a problem.
This is what I found that you could use as an alternative: DateTime::setISODate
$date = explode('-','2000-5-1');
$newDate = new DateTime();
$newDate->setISODate($date[0], $date[1], $date[2]);
setISODate will take the year, week and day as explained in the documentation to create a DateTime object.
I have a date in oW format. This is the 4 digit year in ISO 8601 format followed by the week number. As an example, 201301 represents the week starting on Monday, Dec. 31, 2012.
How can I convert this back to a timestamp. Going from timestamp to string works with date('oW',$ts). How do I go backwards? I am interested in getting the Monday of the week represented in oW format. It would be very silly if PHP provided a way to go in one direction ut did not bother with the inverse.
ISO 8601 expects week dates in the following format:
YYYY\WWW
For example:
2012W52
is the start of the 52st week in 2012 what is actually the 2012/12/24.
To convert week dates back into a timestamp you can use the php function strtotime(). The code will look like follows:
$oW = '2012W53';
$time = strtotime($oW);
// will output: 2012-12-31T00:00:00+01:00 (I'm in CEST)
echo date('c', $time);
However the format you posted above - 201300 - cannot being understood by strtotime(). There are two problems :
00 is not allowed for the week. Allowed values for week are from 01 to 53
You missed the 'W' in the middle. But it belongs to the ISO 8601 standard.
In my tests I further figured out, that 2012W53 points to the 2012/12/31 not 2013W00 (or W01 as you may think)
Further you may read this comment in the php manual and the Wikipedia article on this.
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