Output of php function "strtotime()" is confused - php

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)"

Related

PHP 6 digit date code display in human readable format

I have a set of dates that are formatted like this...
197402
192201
184707
The first four digits represents the year and the remaining two the month. I am trying to output these in this format
February 1974
January 1922
July 1847
I have tried passing it to the date function like this...
echo date ('F Y', 197402)
But this is giving me January 1970 everytime so I assume I have misunderstood how the date function works, can anyone help?
You're getting "January 1970" as an output, because you tried to create a date from the timestamp 197402, which is seconds from January 1st, 1970. If you output the full string from that (with seconds and whatnot), you'll see it's a valid timestamp, producing an actual date, but they all end up in the start of January 1970, see this online demo.
That format, YYYYMM, isn't a recognizable format for most functions. You need to split it up, if you know the format will be in that way - and use that data instead. You can use substr() to split the string, and then convert the numerical month to the string associated with that month, with the help of date() and mktime() (since you just specify the year and month).
The following snippet
$arr = [197402, 192201, 184707];
foreach ($arr as $v) {
$year = substr($v, 0, 4);
$month = substr($v, 4, 2);
echo date("F Y", mktime(0, 0, 0, $month, 0, $year))."<br />"; // mktime() produces a valid timestamp based on just month and year
// Alternatively, drop mktime() and use strtotime() and create from a standard format,
// while specifying a date in the month (which won't matter to the output)
// echo date("F Y", strtotime("$month/01/$year"))."<br />";
}
will output
February 1974
January 1922
July 1847
Alternatively, you can use the DateTime class (which is a lot simpler to work with), and create from a given format with date_create_from_format()
foreach ($arr as $v) {
echo date_create_from_format('Yh', $v)->format('F Y')."<br />";
}
This will generate the same output as above.
References
http://php.net/substr
http://php.net/mktime
http://php.net/date
http://php.net/datetime.createfromformat
I'd use the DateTime class, you can create from a specific format, and then output to another.
As pointed out in the comments below, you also need to set the day to the first of the month, otherwise you'll get undesired results if the current day is greater than the number of days in the given month.
echo DateTime::createFromFormat('Ymd', 19470201)->format('F Y');
As you are passing an int to date, it is considering it as a Unix Timestamp.
To create a date object from a predefined format, use DateTime::createFromFormat.
echo DateTime::createFromFormat('Ym',198403)->format('F Y');
results in
March 1984
You'd need to add a day to it, e.g. just add "01" and then use strtotime to convert that into a unix timestamp, as the date() function expects a timestamp as the parameter.
e.g.
echo date('F Y', strtotime("19220101"));
You have text representation of dates in a non-standard format. First you have to parse them and convert them to timestamps (the number of seconds since Jan 1, 1970 00:00:00 UTC). The PHP function date() can work only with timestamps.
The best approach (as of 2017) is to use the DateTime PHP class for date & time processing:
foreach (array('197402', '192201', '184707') as $text) {
$date = DateTime::createFromFormat('Ym', $text);
echo($date->format('F Y')."\n");
}
The method DateTime::createFromFormat() parses a string using the given format and creates a new DateTime object if the parsing succeeds. It is the OOP equivalent of strtotime() but smarter (because it can get hints about what date components to search in the input string.)
The method DateTime::format() produces the text representation of a date using the provided format. It is the OOP equivalent of date().
The OOP approach (the DateTime* classes) is recommended (and better than the procedural approach) because has built-in support for timezones (the procedural date-time functions lack it.)

Getting day of week from timestamp PHP

i am trying to get the day of week from a timestamp:
an example of the timestamp could be:
2014-09-14 18:28:11
I have tried with the following code:
$date = date("D", strtotime($activity[$i]['timestamp']));
However the result i get here is:
Thu
which should have been sunday?
Also is it possible to get it as a full discription instead of a short version of the day name?
Answer to part two of the question is that you can just use l (lowercase 'L') and it'll output Sunday instead of Sun.
$date = date("l", strtotime($activity[$i]['timestamp']));
As for the first part, it probably output Thursday, 1 January 1970 because it received an error instead of an actual date as argument to strtotime.

Date / Time showing odd value in PHP

I have wierd issues with time / date in PHP this year. Code have not changed at all and my dates are bugged.
Code is for example:
$date = strtotime($order['date']);
$dateNew = date('Y-m-d h:i A', $date);
print $dateNew;
Returns 1969-12-31 07:00 PM for some reasson, altough:
print $order['date'];
Returns 2013-01-12 18:25:43
I'm confused because I'm quite sure that my code is correct.
I dare you to solve this bugger!
The function strtotime() was made for transform English into date format.
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
As i don't know what is really into your $order variable i will suggest 2 solutions :
Maybe you can avoid the strtotime function and replace it by date() directly like this :
$order = ['date' => '2013-01-12 18:25:43'];
$date = date($order['date']);
It works well here: http://codepad.viper-7.com/cbNA87
Or, if it's not working consider to use mktime(), it will convert the date into seconds since the epoch.
The Unix epoch is the reference point for all time stamps. PHP calculates the times from this date in seconds.
The $date should be null and your server in the east coast of the US so it's returns the epoch :)
PHP returns the date 1969-12-31 when there is not a proper date. So if you did
$date = 0;
$dateNew = date('Y-m-d', strtotime($date));
Your result would be 1969-12-31, since that is the default Unix epoch time. http://php.net/manual/en/function.time.php
Unexpected dates of "1969-12-31 07:00 PM" means something went wrong with date() .
your strototime($order['date']) is probably returning false (failing to parse it to a unix timestamp).
Try this and ensure its returning an int (not false)
var_dump($order['date'], strtotime($order['date']));
See the error state of date: http://php.net/date
See the return values of strtotime: http://php.net/strtotime

Date('now') in PHP

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

Problem in fomating date, when using date function with strtotime function

I am using date function along with strtotime function to format the date.
Ex:
<?php
$input_date = '03-JUL-09 14:53';
$formatted_date = date("Y-m-d H:i:s", strtotime($input_date));
echo $formatted_date;
?>
Gives me a expected output: 2009-07-03 14:53:00
while if the input is changed to(I am removing the minute part)-
<?php
$input_date = '03-JUL-09 14';
$formatted_date = date("Y-m-d H:i:s", strtotime($input_date));
echo $formatted_date;
?>
The output here is 1970-01-01 05:30:00, which is not at all expected.
Whats the mistake I am doing and how it can be fixed.
Thanks
Amit
1970-01-01 05:30:00 is the time in India at the Unix epoch (1970-01-01 00:00:00 GMT/UTC). This means that strtotime returned either 0, or false, which was converted to 0 by date. This is because it could not process its input, as others have explained.
The function strtotime expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp.
You can use strings like
03 july 2009
now
09-07-03
09-07-03 14:00:00
+1 week 2 days 4 hours 2 seconds
last Monday
But not incomplete date format like "09-07-03 14". The parser don't understand it and returns nothing so when you call date("Y-m-d H:i:s", strtotime($input_date)); it returns the 0 timestamp (January 1 1970 00:00:00 UTC), cause it's the default value.
You have to give a right string that can be parsed.
This looks like it comes down to the inability of strtotime() to determine which numbers are what in your 2nd date string.
My best advice is to normalize your date-time value before passing it to strtotime(). Something simple like this might be enough
if ( false === strpos( $input_date, ':' ) )
{
$input_date .= ':00';
}
03-JUL-09 14 does not seem to be a valid date string, 03-JUL-09 14:00 works fine
from the php manual: strtotime — Parse about any English textual datetime description into a Unix timestamp
1970-01-01 is a typical baseline date (in ctime for example). I believe the output is formatting a value of 0 into a date, thus the 1970, and the unrelated 05:30:00 time of day.
I believe this is a fluke in attempting to format a date that does not include all the requested time elements. Can you pad the date data to generate a complete input date?
strtotime() fails to parse the second string when you remove the minute part. It doesn't know what the extra "14" means so it returns bool(false). Calling date(false) returns the same thing as date(0), i.e. it gives you the date of the UNIX epoch: January 1, 1970 00:00:00 GMT adjusted for your local timezone.

Categories