strftime() suddenly stopped working. Any insights? - php

I have a simple script which takes an input date, formats it, and stores it in the database. I was using the strftime function in the following way:
$pdate = strftime('Y-m-d', strtotime($_POST['post_date']));
For some reason, this suddenly started returning 'Y-m-d'. Yes, it was returning the format string I was passing it as the first argument! No date information at all. I also tried doing this by passing it a straight-up unicode timestamp as the second argument, but it still just returned the format string. It was working fine until a few days ago.
Now I've switched it to use the date() function instead:
$pdate = date('Y-m-d', strtotime($_POST['post_date']));
Everything works fine now! I'm just wondering if anyone has any ideas why the strftime() function suddenly stopped working. It seems really strange and it's going to bug me all day.

Actually the question might be opposite :) I don't know how did it return correctly formated date, because generally it should be like this:
$pdate = strftime('%Y-%m-%d', strtotime($_POST['post_date']));
As it's described here :)

You are using wrong Date format in strftime parameter. Check strftime function. It should be like this:
$pdate = strftime('%Y-%m-%d', strtotime($_POST['post_date']));
and there are many other formats as well.

Related

PHP DateTime::createFromFormat DATE_ATOM ISO 8601

I am using PHP 7.2, and trying to create a date from a string as follows:
$dateString = '2018-12-31T01:01:01+00:00';
$converted = DateTime::createFromFormat(DATE_ATOM), $dateString);
The snippet above works fine and returns the expected result.
The problem happens when I swap the day and month in the date string provided above, as follows:
$dateString = '2018-31-12T01:01:01+00:00';
$converted = DateTime::createFromFormat(DATE_ATOM), $dateString);
I was expecting this second example to return false, but instead I get an actual date time, 2020-07-12 01:01:01.000000.
So, I have no way of telling whether the second date was a proper date or not, because the system accepted it, and I will be saving in my database something which I shouldn't.
Could this be considered a bug in PHP 7.2?
Could this be considered a bug in PHP 7.2?
No, this is how the method is intentionally implemented. You can argue if it's the right way to implement it, but that's how it is. Notably even if we all agree that it's not right here, it will still keep working as it's implemented.
The practical answer here is to write a validator (for entirety of format and all parts) and run any untrusted input through it.

PHP time() function returns the epoch instead of the current time

I have the following php code that is meant to format the current date and time:
$rawdatetime = time();
$datetime = date('Y-m-d', $rawdatetime) . 'T' . date('H:i:s', $rawdatetime) . '.000Z';
$this->debug($datetime);
The formatting seems to be working fine, but it keeps outputting that it is 1970; I get the following output:
1970-01-01T00:00:00.000Z
My guess is that my server is not configured properly, but my Google search has not given me any clues. I am running WAMP if it helps.
Thanks in advance for any advice you may have.
EDIT: it seems that the date and time functions are working properly; but assigning them to a variable is what is the problem. Any work arounds to get the same formatting as above would be welcomed. But I would also like to know why this problem is happening.
Firstly, you don't need to use time() at all here, because date() will use the current time as the default value if you don't pass a value to that parameter.
Secondly, you're using two separate date() calls, separated by a "T". Note that the formatting for date() can accept a hard-coded character like T; you just need to escape them with backslashes, so you don't need to split it into two function calls.
Your entire code could look like this:
$datetime = date('Y-m-d \T H:i:s.\0\0\0\Z');
Which gives 2013-06-19 T 11:18:53.000Z
It works perfectly for me (when echoing, instead of $this->debug). So either you somehow have a faulty PHP version, or the problem is not in your code sample. This is what I did:
<?php
$rawdatetime = time();
$datetime = date('Y-m-d', $rawdatetime) . 'T' . date('H:i:s', $rawdatetime) . '.000Z';
echo $datetime,"\n";

Php Date Function

I have tried using php date function() like as follows
$date=date('Y-m-d').' '.date('H:i:s');
echo $date;
the output displayed is 2013-04-03 09:04:02.. but my system is 02:49 pm...
What time is being displayed for me? I tried changing the internet timing even then I am getting the same answer ?
First off, it is not necessary to use the date function twice. This will do the same thing:
echo date('Y-m-d H:i:s');
Second, you need to set PHP's date.timezone. This can be done in the php.ini file, but it can also be done using the date_default_timezone_set function, like this:
date_default_timezone_set('Europe/Amsterdam');
The string that you have to put in can be found in the documentation.
It may also be worth noting that you can tell the date function to use any time. This is done by passing in a *nix timestamp as the second argument. For example:
// One week ago from now
echo date('Y-m-d H:i:s', time()-604800);
It will show server's time only. If possible compare with your server time. If you want to use local machine's time you need to go with JAVASCRIPT.
And another suggestion,
You don't have to use individually to display date & time. You can achieve this in a single statement like this.
$date=date('Y-m-d H:i:s');
You will get the same format 2013-04-03 09:04:02
check for your system timezone and your default timezone in php by opening phpinfo()

DATE php and mysql unique

Ok, my problem is very weird
I have this code in php passing three variables day month and year
$MONTH=$_POST['day'];
$DAY=$_POST['month'];
$YEAR=$_POST['year'];
$newdate="$YEAR/$DAY/$MONTH";
$DATEOFBITH=date("Y-m-d H:i:s", strtotime($newdate));
the output of $newdate:
1967/1/1
looks good but when I look at the mysql it shows zeros
however if I write the code like below. and insert it to mysql it works!
$newdate="1986/1/1";
$DATEOFBITH=date("Y-m-d H:i:s", strtotime($newdate));
I tried everything still not get it!
if also did comparison
$MONTH=$_POST['day'];
$DAY=$_POST['month'];
$YEAR=$_POST['year'];
$olddate="$YEAR/$DAY/$MONTH";
$newdate="1986/1/1";
if($newdate==$olddate){
echo "the same";
}
and it output:
the same
so, what is the problem?
when I use the POST, it does not work for mysql
but if I enter the date manually it works fine!
I tried trim/(string) none works :(
Change:
$newdate="$YEAR/$DAY/$MONTH";
To:
$newdate=$YEAR.'-'.$MONTH.'-'.$DAY;
Basically, you need to use a SQL standard date format to make sure PHP does not "guess" the format.

Human readable date into timestamp

I am trying to parse a logfile, using human readable dates without a year into a timestamp. I have looked over the function strtotime() but haven't had any success with it.
Example time: Apr-26-10:49:36
which is the equiv of "M-j-H:i:s" for the date() function.
Edit: without a year, in my case here..its perfectly fine to assume the year is the current year.
I've created a script to break this down in the past, but it was long and redundant. I was hoping for a more simplified way of doing this. Any help, or pointing in the right direction would be greatly appreciated :D
If you know the format of the date, you can use strptime to parse it. This returns an array that you can use to determine the arguments for mktime.
Maybe not the prettiest way to do it but it works
The problem why the strtotime function is not working is because the hyphen between the Day and the Hour 26-10.
You can replace the - and then use strtotime.
echo date('Y-m-d H:i:s', strtotime(str_replace('-', ' ', 'Apr-26-10:49:36')));

Categories