I have 2 values stored in a database. One is a startdate, the other an enddate.
Now when the Year, month and date of the 2 are equal. I would like the enddate to only show the time.
For example
maandag 16 juni 14:06 tot maandag 16 juni 14:15
would be
maandag 16 juni 14:06 tot 14:15.
I'm pretty new at PHP. I've tried the following statement, but i'm pretty sure it's incorrect:$
<?php if($item->startdate("Ymd") == ($item->enddate("Ymd"))): ?>
The type of the values are DateTime
You could also try using the substr-function, only comparing portions of the variables:
if(substr($item->startdate, 0, 10) == substr($item->enddate, 0, 10))
Here we compare the first ten characters (YYYY-MM-DD) of $item->startdate and $item->enddate.
<?php if(date('Y-m-d',strtotime($item->startdate) == date('Y-m-d',strtotime($item->enddate))) : ?>
Try
if(date('Ymd',strtotime($item->startdate)) == date('Ymd',strtotime($item->enddate))) :
The date function should take almost any data format and reformat it to the format you give it i.e. 'Ymd'
PHP Manual for the date() function
Related
I want to remove zeros in PHP date for 1-9.
My Code:
//Note : I change to Indonesian Format date.
$hari = array ("Minggu","Senin","Selasa","Rabu","Kamis","Jumat","Sabtu");
$bulan = array ("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
$waktu[0]=$hari[date("w",time())];
$waktu[1]=date("d",time());
$waktu[2]=date("m",time());
$waktu[3]=date("Y",time());
$waktu[4]=date("H",time());
$waktu[5]=date("i",time());
$waktu[6]=date("s",time());
$hariini="$waktu[0]";
$tanggalini="$waktu[1] ".$bulan[$waktu[2]-1]." $waktu[3]";
$jamini="$waktu[4]:$waktu[5]:$waktu[6]";
$today = $waktu[0].", ".$tanggalini;
echo $today;
the result of this is Kamis, 01 Februari 2018.
See here for the numbers 1 through 9 will have 0 to the left.
How can I turn it into Kamis, 1 Februari 2018, without 0?
use
$waktu[1]=date("j",time());
instead of
$waktu[1]=date("d",time());
Use date format j for retrieve date without leading 0
change
$waktu[1]=date("d",time());
to
$waktu[1]=date("j",time());
Use the following
$waktu[1]=date("j",time());
I am trying to check if one date is equal than the other date, but I can't get the match because the date format coming from the form turns into a different order once it gets through the "parse" code.
I need to format this date to find the match, here is a sample code to show how I am trying:
...
// $ago will give me this date: 2016-12-09 00:00:00
$ago = Carbon\Carbon::today()->addDays(2); // Todays date + 2 days
//$request->datex has the date coming from a form with this format, '12-06-2016'.
// Once a parse $request->datex here, the date gets out of order:
$my_date = Carbon\Carbon::parse($request->datex);
// it shows the date like this, 2016-09-12 00:00:00 , I need it to be on this format: 2016-12-09 00:00:00
// then I could do this:
if ( $ago$ == $my_date ) {
dd($my_date.' is equal to: '.$ago );
}else{
dd(' Not equal!');
}
...
Thanks for looking!
Change this line
$my_date = Carbon\Carbon::parse($request->datex);
with this:
$my_date = Carbon::createFromFormat('m-d-Y', $request->datex)
I've assumed that your format '12-06-2016' means DAY-MONTH-YEAR
UPDATE
Tested my solution on my machine and it works, date is recognized properly:
When
$request->datex = '12-06-2016'
then
$my_date = \Carbon\Carbon::createFromFormat('m-d-Y', $datex);
gives me date like that: public 'date' => string '2016-12-06 18:52:09.000000' (length=26)
Date has been parsed properly. The thing that I've assumed just now. These dates won't be same cause of hours, minutes, seconds and milliseconds. To fix that just we have to compare dates that way:
if ( $ago->format('Y-m-d') == $my_date->format('Y-m-d') )
//do something awesome with our equal dates
PHP expects DD-MM-YYYY or MM/DD/YYYY formats.
If you always have a MM-DD-YYYY format, you could do this before parsing:
$request->datex = str_replace('-', '/', $request->datex);
I have a date calendar in my php form which gives me date '19-05-2014'. I should compare this date to database time-modified (1400481271) exactly.
When converted '19-05-2014' to UNIX TIME STAMP, but I get the result as 1397858400 which was a wrong time stamp.
id userid timemodified
370 23 1400481271
329 24 1427771915
333 30 1428309816
332 32 1428303307
327 33 1427689703
328 34 1427710711
<?php
if ( preg_match('/^(?P<day>\d+)[-\/](?P<month>\d+)[-\/](?P<year>\d+)$/', '19-05-2014', $matches) )
{
$timestamp = mktime(0, 0, 0, ( $matches['month'] - 1 ), $matches['day'], $matches['year']);
echo $timestamp;
}
?>
The date you are trying to match is, converted to timestamp:
1400457600
If you got a different value you converted it wrong (maybe switched day and month or something similar).
It is, however, while close, still not identical to the given value 1400481271 in your database.
The reason for this becomes clear if you convert it back to a readable date:
05/19/2014 # 6:34am (UTC) 1400481271
For comparison, the given date:
05/19/2014 # 12:00am (UTC) 1400457600
As you can see there is a difference because it's the same date, but a different time.
The easiest way would be to retrieve only the date portion from the database.
You can do this in your SQL query like this:
SELECT DATE( FROM_UNIXTIME( timemodified ) ) AS modified
This will return 2014-05-19, which you can easily compare with your string.
Or, if you can even retrieve it in the same format you get it, so you don't need to rewrite your date with regex:
SELECT DATE_FORMAT( FROM_UNIXTIME( timemodified ), '%d-%m-%Y' ) AS modified
This will return 19-05-2014 for 1400481271.
The following code works :
$d1 = new DateTime("12/31/2015");
which corresponds to the 31th of December 2015.
I take out some date from a database, with the following format : day/month/year. So for example, I get :
31/12/2015 - 31th December 2015
02/06/2015 - 2nd June 2015
29/03/2015 - 29 March 2015
....
The following code didn't work :
$d2 = new DateTime("31/12/2015");
The nature of my data are : string, and I would like to get the timestamp (this is my final goal but not the matter in this case) for each of these dates.
Question : Can we initialize DateTime with day-month-year order instead of month-day-year ?
Requirement : Use of DateTime object only (older object are not allowed like date()).
You need to format your date after:
$date = DateTime::createFromFormat('M/d/Y', '12/31/2015');
echo $date->format('d-m-Y');
look at this page, it could help you:
http://php.net/manual/fr/datetime.createfromformat.php
You can also select your date in french in SQL, for example:
select date_format(leChampDate, '%d/%m/%Y') as dateFrance
I have a form where users can enter a birthdate. If a date prior to 1970 is entered, e.g. 1964, it is saved in the database as 2064. Can anyone shed some light on what is happening? Thanks. I am formatting the dates for MySQL using the function below:
function formatdate( $s ) {
$date = date_create($s);
return date_format($date, 'y-m-d');
}
MySQL assumes that years less than 70 (when not given as a 4 digit year) are in the 21st century and not the 20th. You should format as a 4 digit year to avoid this problem:
return date_format($date, 'Y-m-d');
Use a capital Y in your format mask to return a 4 digit year.