I just tried,
date_create_from_format('Ym','201302')
And I guess because it's the 29th today, it's actually giving me back March 1st.
I was hoping to get back 2013-02-01 00:00:00.
Is there a different function that will parse a date "correctly"? If not, I can extract it myself, not a big deal.
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
If format contains the character !, then portions of the generated time not provided in format, as well as values to the left-hand side of the !, will be set to corresponding values from the Unix epoch.
The Unix epoch is 1970-01-01 00:00:00 UTC.
(DateTime Manual)
So, adding a ! at the beginning of your format string should fix your problem.
TheWolf's solution seems to work perfectly, but here's an alternative I started writing anyway:
function CompactStrToTime($str) {
$year = strlen($str)>=4 ? substr($str,0,4) : date('Y');
$month = strlen($str)>=6 ? substr($str,4,2) : 1;
$day = strlen($str)>=8 ? substr($str,6,2) : 1;
$hour = strlen($str)>=10 ? substr($str,8,2) : 0;
$min = strlen($str)>=12 ? substr($str,10,2) : 0;
$sec = strlen($str)>=14 ? substr($str,12,2) : 0;
return mktime($hour,$min,$sec,$month,$day,$year);
}
Related
I'm trying to do a simple if echo statement.
<?php if (time("mm-dd") > strtotime("11-01") && time("mm-dd") < strtotime("02-28"))echo 'stuff' ?>
Basically I want to echo something if today is either Nov, Dec, Jan, Feb. The code works if I use time() and the full year but I'd like to just compare the month, day. I think I have some silly syntax error that I just can't figure out. This code snippet is placed in the <head> of my html if that makes a difference. Little help. Thanks.
This is what I ended up with. Thanks!
<?php if ($today > '12-16' || $today < '01-08') echo 'yes' ?>
with $today = date("m-d")
Use the date function instead of time.
Switch && to ||. No m-d date string will ever be both greater than 11-01 and less than 02-28.
You probably want to use an inclusive comparison operator with November 1 and an exclusive one against March 1 to account for leap years.
Instead of calling date() twice, why not assign the result to a variable?
Here it is all together:
$today = date('m-d');
if ($today >= '11-01' || $today < '03-01') { ... }
Consider using date checking the month only:
$month = (int) date("m");
$isMonthCorrect = $month === 11 || $month === 12 || $month === 1 || $month === 2;
Note the importance of (int). Integer comparisons are more reliable than string comparisons, even if they behave similarly.
Or if you want to check between two dates, to optimize performance, you should evaluate the dates into timestamps before putting them in code.
For example, you can use some websites for converting Unix timestamps. (Not gonna advertise any here, but you can search "Unix timestamp converter) You can also use php -r to get quick output:
php -r 'echo strtotime("2016-11-01 00:00:00");'
php -r 'echo strtotime("2017-02-01 00:00:00");'
Then you can use them like this:
$minimum = 1477958400; // from first command line
$maximum = 1485907200; // from second command line
$isInPeriod = $minimum <= time() && time() <= $maximum;
Keep in mind:
time() always returns the current Unix timestamp, i.e. number of seconds since the Unix Epoch. Use strtotime for converting a string to time, and use date() to convert a timestamp to a string in a given format.
Unix timestamp is always absolute. You can't convert a "month" into a Unix timestamp. You can only obtain the current Unix timestamp with time(), or get specific data from the timestamp using date().
References:
strtotime
time
date
I got data from a Persavive V12 Db. The dates are in a weird format.
They are like float :
Example of date in table : 39787.0
Do someone know this kind of format ? It seems to be the number of days from 1901-01-01.
Since the dates are float, is it possible to add days from a date in PHP ? Like :
$from = '1901-01-01';
$date = date('Y-m-d', strtotime($from .' +'. $float .' days'));
I got always 1970-01-01. Is there another way ?
My first answer so don't hurt me much.
I'm not quite sure what you want to do (put back in the database or perform further operations so I only posted one solution)
The number you posted 39787.0 is a unix timestamp. You need to use mktime to convert what you want to a number (And you need date to convert it to a readable form). Since mktime uses 1901-01-01 as a starting date as well you have to make a couple of changes.
I was a biz lazy and didn't include all the code for all fields but this should allow you modify to add whatever you want (and possibly clean it up to a single line if you want).
I made it in long form so it's easy to read.
<?
$olddate = 39787.0;
$hour = 0;
$min = 0;
$sec = 0;
$month = 0;
$year = 0;
$day = 12;
$month += date("m",$olddate);
$day += date("d",$olddate);
$year += date("Y",$olddate);
$x = date("Ymd",mktime($hour,$min,$sec,$month,$day,$year));
echo $x;
?>
If 39787.0 is in the database, what value is that in a real date (using the application)? If it's 2008-12-05, the it's a VB Date. Do you know what the application was written in? I've seen people use the VB Date data type and store it in an 8 byte float in Btrieve (back in 2003). If it's a VB Date, 1 is 1899-12-31. You should be able to add the number to 1899-12-30 to get the correct date value. Something like:
<?php
$date = new DateTime('1899-12-30');
$date->add(new DateInterval('P39787D'));
echo $date->format('Y-m-d') . "\n";
?>
this code keeps telling me that $lasUpdate is always greater than $yesterday no matter the change i make to $yesterday result is (12/31/14 is greater than 01/19/15 no update needed). i feel like i'm missing something simple thank you in advance it is greatly appreciated.
$result['MAX(Date)']='12/31/14';
$lastUpdate = date('m/d/y', strtotime($result['MAX(Date)']));
$yesterday = date('m/d/y', strtotime('-1 day'));
if($lastUpdate<$yesterday){echo $lastUpdate.'is less '.$yesterday.'<br>'.'update needed';}
if($lastUpdate>=$yesterday){echo $lastUpdate.'is greater than '.$yesterday.'<br>'.'no update needed';
You have fallen victim to PHP type juggling with strings. A date function has a return value of a string. You cannot compare dates in their string format since PHP will juggle strings into integers in the context of a comparison. The only exception is if the string is a valid number. In essence, you are doing:
if ('12/31/14' < '01/19/15') { ... }
if ('12/31/14' >= '01/19/15') { ... }
Which PHP type juggles to:
if (12 < 1) { ... }
if (12 >= 1) { ... }
And returns false on the first instance, and true on the second instance.
Your solution is to not wrap date around the strtotime functions, and just use the returned timestamps from the strtotime functions themselves to compare UNIX timestamps directly:
$lastUpdate = strtotime($result['MAX(Date)']);
$yesterday = strtotime('-1 day');
You will however want to use date when you do the echo back to the user so they have a meaningful date string to work with.
Try something like this:
$lastUpdate = strtotime($result['MAX(Date)']);
$yesterday = strtotime('-1 day');
if ($lastUpdate < $yesterday) { /* do Something */ }
12/31/14 is greater than 01/19/15
Because 1 is greater than 0. If you want to compare dates that way you will need to store them in a different format (from most to least significant digit), for example Ymd.
Or store the timestamps you are making in the different variables and compare them.
I have a string "date" which can be DD.MM.YYYY or D.M.YYYY (with or without leading zeros), it depends what a user types.
Then I use it in a condition to send another email when the day is today.
if($_POST["date"]== date("d.m.Y")){
$headers.="Bcc: another#mail.cz\r\n";
}
The problem is that the mail is send when the date format DD.MM.YYYY (with leading zeros) only.
My proposed solution
As I'm not very good in PHP, I only know the solution theoretically but not how to write the code - I would spend a week trying to figure it out on my own.
What's in my mind is dividing the date into three parts (day, month, year), then checking the first two parts if there's just one digit and adding leading zeros if it's the case. I don't know how to implement that to the condition above, though. I have read a few topics about how to do this, but they were a bit more different than my case is.
You should equalize to same format d.m.Y and you can do this with strtotime and date function:
$post_date = date("d.m.Y", strtotime($_POST["date"]));
if($post_date == date("d.m.Y")){
$headers.="Bcc: another#mail.cz\r\n";
}
I changed date to $post_date for more clear. I'll try to explain difference with outputs
echo $_POST["date"]; // lets say: 8.7.2013
echo date("d.m.Y"); // 09.09.2013 > it's current day
strtotime($_POST["date"]); // 1373230800 > it's given date with unix time
$post_date = date("d.m.Y", strtotime($_POST["date"])); // 08.07.2013 > it's given date as right format
If you use date function without param, it returns as current date.
Otherwise if you use with param like date('d.m.Y', strtotime('given_date'));, it returns as given date.
$post_date = date("d.m.Y", strtotime($_POST["date"]));
At first, we converted your date string to unix with strtotime then equalized and converted format that you used in if clause.
first set date format with leading Zero
$postdate = strtotime('DD.MM.YY', $_POST['date']);
and also matching date will be in same format
$matching_date = date('DD.MM.YY', strtotime('whatever the date'));
then
if ( $postdate === $matching_date )
{
// send mail
}
Why don't you just check the length of the _POST (it can be either 8 or 10)
if (strlen($_POST["date"]) == 10) {
$headers.="Bcc: another#mail.cz\r\n";
}
if($_POST['syear']){
$compy = strtotime($_POST['syear']);
if(date("Y") <= date("Y", $compy)){
//success
$startdate = $_POST['syear'];
}
else{
$error = 6;
}
}
I have created the above code and have no idea where I have gone wrong. I am posting a string from a form with a number in it and want to compare it to the current year. If the number is equal to or less than the current year it is supposed to be a success. It is always a success even if the number is larger than the current year. Do I need to convert some strings to ints or have I missed something entirely.
PHP handle string comparison very well, did you try this directly ? (and changing the comparison order to >=)
if($_POST['syear']){
if(date("Y") >= $_POST['syear']){
$startdate = $_POST['syear'];
}else{
$error = 6;
}
}
You cannot convert simply a year to time. Using your example as is, you need to have a string of yyyy/mm/dd format to use strtotime. If you are really just checking year, you can use January 1st as a check date.
$compy = strtotime($_POST['syear'] . '-01-01' );
You can compare integers or strings the same way. If you want to be sure about the type comparison you can always cast the variable but in this case it's pointless.
2012 is inferior to 2013. "2012" is inferior to "2013". date( 'Y' ) returns a 4 characters string you can compare with your $_POST['syear'] if it's a 4 character string. Hope this helps.
You could try
if(time() <= $compy){
since your already doing strtotime() on whatever compy is originally. This way your working with 2 unix timestamps and comparing them that way.