Reputation:
I did write this script in javascript, to get the date output. Now that I have changed the background script to PHP I need the code to get the date in below format.
I also tried to get the date but was getting the previous day and not the current one.
Date.prototype.toFormattedString = function (f)
{
var nm = this.getMonthName();
var nd = this.getDayName();
f = f.replace(/yyyy/g, this.getFullYear());
f = f.replace(/yy/g, String(this.getFullYear()).substr(2,2));
f = f.replace(/MMM/g, nm.substr(0,3).toUpperCase());
f = f.replace(/Mmm/g, nm.substr(0,3));
f = f.replace(/MM\*/g, nm.toUpperCase());
f = f.replace(/Mm\*/g, nm);
f = f.replace(/mm/g, String(this.getUTCMonth()+1).padLeft('0',2));
f = f.replace(/DDD/g, nd.substr(0,3).toUpperCase());
f = f.replace(/Ddd/g, nd.substr(0,3));
f = f.replace(/DD\*/g, nd.toUpperCase());
f = f.replace(/Dd\*/g, nd);
f = f.replace(/dd/g, String(this.getUTCDate()).padLeft('0',2));
f = f.replace(/d\*/g, this.getUTCDate());
return f;
};
Date.prototype.getMonthName = function ()
{
return this.toLocaleString().replace(/[^a-z]/gi,'');
};
Date.prototype.getDayName = function ()
{
switch(this.getDay())
{
case 0: return 'Sunday';
case 1: return 'Monday';
case 2: return 'Tuesday';
case 3: return 'Wednesday';
case 4: return 'Thursday';
case 5: return 'Friday';
case 6: return 'Saturday';
}
};
String.prototype.padLeft = function (value, size)
{
var x = this;
while (x.length < size) {x = value + x;}
return x;
};
var now = (new Date()).toFormattedString('yyyymmdd')
I need output like 20150221
Upvotes: 0
Views: 106
Reputation: 3681
$now = new DateTime()->format('Ymd');
Read the PHP documentation on the DateTime class and date function.
Upvotes: 0
Reputation: 619
Just add the below code to get the date in PHP
<?php
echo date('Ymd');
?>
EDIT: The format is 20150221
Upvotes: 1
Reputation: 11808
Local System Date:
If your script needs date of the local system i.e. the system of the person who will use your application via browser, then you should go for JavaScript date and not PHP. In this case, the system date may differ as users will access it from different region. Also, they can adjust their system date.
var date = new Date();
var time = date.getTime(); //output e.g. 1424497935248
Server System Date:
If your script needs the server date, irrespective of the date of local system, then you should go for PHP date.
echo date("Y-m-d h:i:sa"); //sample output 2015-02-21 06:00:15am
echo time(); //sample output 1424498415
Upvotes: 0
Reputation: 1816
You could also try using Carbon, it's really easy and it makes easier the development, you can check it out in here https://github.com/briannesbitt/carbon
Here's an example of the usage of carbon
<?php
printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); //implicit __toString()
$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
The documentation explains it all with great detail, it's not that hard.
Upvotes: 0
Reputation: 7791
Use date like this :
<?php
$current_day = date('Ymd');
echo $current_day;
?>
You can find more options in
Upvotes: 0