How to remove zeros on PHP date for 1-9? - php

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());

Related

How to get a date in the current format?

How to get a date in the format 5 - 7 Jan
if (get_field('event_from')) {
echo $datefrom->format('d.m.Y').' - ';
}
return 21.01.2020 - 22.01.2020
But I need a date in a different format for example 5 - 7 Jan
Please help.
If the ACF-field is a datepicker, you could configure the return format in the field it self.
Setting it to j M will give you the value "5 Jan"
'j' is numeric representation of day without a leading zero.
'M' is a short textual representation of a month, three letters.
Check the PHP-manual for more information:
https://www.php.net/manual/en/function.date.php
Once the field is configured to return the date formated as above, you can just append the value to your string.
$date_string;
if (get_field('event_from')) {
$date_string = get_field('event_from') . ' - ';
}
if (get_field('event_to')) {
$date_string .= get_field('event_to');
}
echo $date_string;

Compare datetime, only show time if Ymd are same

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

How to get a specific date in the current year

Suppose the current year is 2014; then I want get the date 2014-06-30, if the current year is 2015 then I want 2015-06-30
I tried date('Y'); but it is just giving current year.
You can just use:
date("Y-06-30")
Demo : https://eval.in/103028
try:
echo date("Y-06-30");
OUTPUT:
2014-06-30
See date documentation:
http://php.net/manual/en/function.date.php
You can concatenate the rest of your required date to your date() function as follow
echo date('Y') . '-06-30';
//output 2014-06-30
Use this :
date("Y-06-30");
Y will be the your year and rest will same.
Where y is A full numeric representation of a year, 4 digits for more Info : Date()
You need to put like below code:
$d = date('Y');
$data = $d."-06-30";
echo $data;

converting date in php returns different results depending on date format

What seemed to be a fairly standard date conversion is giving me unusual results. I want to get $pay_date into the 'j/d/Y' format, but can only get the correct date if the format is 'Y-m-d'.
Here's my code:
$payment = '2014-09-09';
$pay_date = strtotime("+1 month", strtotime($payment) );
$converter = date('j/d/Y', $pay_date );
echo $converter;
result: 9/09/2014 (should be 10/09/2014)
If I keep these variables, but change $converter to this format:
$converter = date('Y-m-d', $pay_date );
the result is correct - 2014-10-09
I also tried this:
$convert2 = DateTime::createFromFormat('Y-m-d', $pay_date)->format('j/d/Y');
echo $convert2;
result: 9/09/2014
but:
$convert2 = DateTime::createFromFormat('Y-m-d', $pay_date)->format('Y-m-d');
echo $convert2;
gives me the correct result: 2014-10-09
j and d are the same formatting options, they both represent DAYs; from the manual for PHP's date function:
d Day of the month, 2 digits with leading zeros 01 to 31
j Day of the month without leading zeros 1 to 31
Use the format 'm/d/Y'.

PHP: date function to get month of the current date

I want to be able to figure out the month of the current date variable. I'm ex vb.net and the way to do it there is just date.Month. How do I do this in PHP?
Thanks,
Jonesy
I used date_format($date, "m"); //01, 02..12
This is what I wanted, question now is how do I compare this to an int since $monthnumber = 01 just becomes 1
See http://php.net/date
date('m') or date('n') or date('F') ...
Update
m Numeric representation of a month, with leading zeros 01 through 12
n Numeric representation of a month, without leading zeros 1 through 12
F Alphabetic representation of a month January through December
....see the docs link for even more options.
What does your "data variable" look like? If it's like this:
$mydate = "2010-05-12 13:57:01";
You can simply do:
$month = date("m",strtotime($mydate));
For more information, take a look at date and strtotime.
EDIT:
To compare with an int, just do a date_format($date,"n"); which will give you the month without leading zero.
Alternatively, try one of these:
if((int)$month == 1)...
if(abs($month) == 1)...
Or something weird using ltrim, round, floor... but date_format() with "n" would be the best.
$unixtime = strtotime($test);
echo date('m', $unixtime); //month
echo date('d', $unixtime);
echo date('y', $unixtime );
as date_format uses the same format as date ( http://www.php.net/manual/en/function.date.php ) the "Numeric representation of a month, without leading zeros" is a lowercase n .. so
echo date('n'); // "9"
As it's not specified if you mean the system's current date or the date held in a variable, I'll answer for latter with an example.
<?php
$dateAsString = "Wed, 11 Apr 2018 19:00:00 -0500";
// This converts it to a unix timestamp so that the date() function can work with it.
$dateAsUnixTimestamp = strtotime($dateAsString);
// Output it month is various formats according to http://php.net/date
echo date('M',$dateAsUnixTimestamp);
// Will output Apr
echo date('n',$dateAsUnixTimestamp);
// Will output 4
echo date('m',$dateAsUnixTimestamp);
// Will output 04
?>
To compare with an int do this:
<?php
$date = date("m");
$dateToCompareTo = 05;
if (strval($date) == strval($dateToCompareTo)) {
echo "They are the same";
}
?>

Categories