strftime in dates? - php

When I had UNIX timestamps, I'd write:
strftime("%A", $date)
But now I have datestamps, like "2011-08-02"
How can I make it output the weekday name, e.g "Sunday"?

You can convert the date stamp to a timestamp using the function strtotime.
Once you have the timestamp, you can just use the function date to show the date in the desired format.

First use the strtotime function to convert the '2011-08-02' to a UNIX timestamp, and then proceed as you usually would
For example, the following are equivalent:
$date = 1312243200; // A unix timestamp
$date = strtotime('2011-08-02'); // The date that it represents
You can then do whatever you would usually do with the result
The strtotime() function is fairly forgiving in what date formats it accepts and even accepts values such as '8pm tomorrow' or 'last Monday' - See http://www.php.net/strtotime

Use date('l'); Add more properties like so: date('l d-m-Y');.
More info here

(2nd August 2011 was a Tuesday, not a Sunday.)
<?php
echo strftime("%A", strtotime("2011-08-02"));
// Output: "Tuesday"
?>
Live demo
strtotime documentation

date('l',strtotime('2011-08-02'));

Related

PHP - strtotime() and datetime strange behavior

I have a some dates formatted as string in this format: 18-04-17.
I want to convert them to dates.
At first I used strtotime() to change it to date:
$timestamp = strtotime($row['reqEndDate']);
$newdate = date("d-m-Y", $timestamp);
echo $newdate;
This outputs: 17-04-2018. As you can see it mistakes the day with the year.
Next I tried to use datetime, as follows:
$newdate = datetime::createFromFormat("d-m-Y", $row['reqEndDate']);
echo $newdate->format('d-m-Y');
This outputs: 18-04-0017 .This method gets the day correctly, but instead of 2017 it prints 0017.
Is there any reason for this behavior? Maybe some settings in my php setup to look for?
Uppercase Y will produce 4 digit year. Lowercase y produces a two-digit year. Your code with DateTime should be:
$date = DateTime::createFromFormat('d-m-y', $row['reqEndDate']);
echo $date->format('d-m-Y');
You can read about supported formats in PHP's manual page about date.

Date from database returns in a different form

I'm trying to get today's date, and compare it to the date in my database, but the date in my database returns in a different form from the date that I get from the date function, so if I compare them in an if statement, the values are always going to be false. Is there a way for me to compare them so that it returns as true?
$date = date('y-m-d'); //date from date function ---> 15/07/19
$dateFromDatabase; //date from database ---> 2015/07/19
if ($date == $dateFromDatabase) {
echo "It's the same day.";
}
strtotime
You have in php some great functions to convert your human readable dates to timestamps.
The first magic function is called strtotime (string to time) : give it your date, you get a UNIX timestamp! Let's see some examples:
echo strtotime('2008-04-12 13:24');
echo strtotime('12 april 2008 13:24');
echo strtotime('12.04.2008 13:24');
And more powerfull, strtotime can recognize some keywords:
echo strtotime('now');
echo strtotime('+4 days');
echo strtotime('+1 month');
echo strtotime('next monday');
echo strtotime('+2 weeks 3 days 4 hours 23 minutes');
The second argument of strtotime is a timestamp, and its default value is the actual timestamp (time()). So echo strtotime('+4 days') is relative to the current time. Of course you can also give strtotime your mysql date! (Note you can also use the mysql function UNIX_TIMESTAMP, which use a bit more ressources).
To compare dates, it's now just a detail:
// your first date coming from a mysql database (date fields)
$dateA = '2008-03-01 13:34';
// your second date coming from a mysql database (date fields)
$dateB = '2007-04-14 15:23';
if(strtotime($dateA) > strtotime($dateB)){
// bla bla
}
Better than substring, isn't it ?!
Here is just another example, not relative to current date but to a particular date:
strtotime('23 hours ago', strtotime('2005-04-13 14:00'));
This mean 23 hours ago relatively to the second given date, which must be a timestamp.
user manual doesn't give a complete description of the supported date formats. Strtotime('dd/mm/YYYY') doesn't work, it works only with mm/dd/YYYY format.
date in dd/mm/YYYY format, can be convert it to YYYY-mm-dd by using explode() function, but I think there are better solutions.
$date = '25/05/2010';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));

How can I compare the date with today's date using UNIX timestamp values in PHP?

I've following variable containing date in MM-DD-YYYY format. I want to compare this date with today's date. If the date containing in a variable is greater than the today's date I want to echo "Error" and if it is less than or equal to today's date I want to echo "Success".
For this thing I don't want to use DateTime class.
I think using UNIX Timestamp values could be a better option. If yo have any other better and efficient option you are welcome.
Following is the variable containing date in MM-D-YYYY format
$form_data['reg_date'] = '12-11-2014'; //This is today's date i.e. 11th December 2014
If the variable $form_data['reg_date'] contains date greater than today's date(i.e. 11th December 2014) it should give error message otherwise should echo success message.
Thanks.
I've following variable containing date in MM-DD-YYYY format. I want to compare this date with today's date.
You cannot compare string representation of date in format m-d-Y. This format is invalid format, and php will not understand it. Read the manual what date formats are valid.
Best way to compare string dates is to have it in format Y-m-d or convert your string date to unix timestamp integer. But once you have date in Y-m-d format, it is trivial to convert it to unix timestamp, so converting it to timestamp just for comparing is an unnecessary step.
Convert m-d-Y to m/d/Y format, and then to unix timestamp:
$date = '12-25-2014';
$date = str_replace('-', '/', $date);
var_dump($date);
var_dump(strtotime($date));
if (strtotime($date) > strtotime('today')) echo "ERROR";
else echo "SUCCESS";
demo
But this I already explained in answer of your previous/same question (see 2nd link).
I think using UNIX Timestamp values could be a better option. If yo have any other better and efficient option you are welcome.
Other method could be converting format with sscanf() function:
$date = '12-25-2014';
sscanf($date, "%d-%d-%d", $m, $d, $Y);
$date = "$Y-$m-$d";
var_dump($date);
if ($date > date('Y-m-d')) echo "ERROR";
else echo "SUCCESS";
demo
But I would still recommend you to use DateTime class, like I already explained if my previous answer.
Please try below code
if(strtotime(date('d-m-Y')) == strtotime($form_data['reg_date'])){
echo 'Today\'s Date';
}
First you have to convert your date in timestamp value like
$date_time = strtotime("11-12-2014");
and then you can do this to compare today date with the date you have.
$diff = $date_time - time();
This will gives you the date difference in seconds.

PHP string to Date and Time

I have the following string: 2010-04-08T12:46:43+00:00
I want to convert that to:
8th April 2010 # 12:46
Is that easy enough?
Take a look at strtotime to create a UNIX timestamp from your time string, and then use date($format, $UNIXtimestamp); to create a normal date again:
$Timestamp = strtotime("2010-04-08T12:46:43+00:00");
echo date("your time format", $Timestamp);
You can look up the specific characters for the time format from PHP.net
Here you go.
EDIT : Exactly as you needed
Code:
$time_value = strtotime("2010-04-08T12:46:43+00:00");
$date_in_your_format = date( 'jS F Y # g:i', $time_value);
echo $date_in_your_format;
yep. use strtotime() + date()
Try taking a look at strtotime() and date().

How to reformat date in PHP?

I am pulling the dates of various posts from a database. The dates are in the following format:
2009-08-12
Numeric Year - Numeric Month - Numeric Day
How can I reformat these dates to something more user friendly like:
August 12, 2009
Numeric Month Numeric Date, Numeric Year
Assuming that the date gotten from the mysql database is stored in a variable called:
$date = $row['date_selected'];
Unlike the strtotime based examples, this allows you to ensure the month and day are interpreted in the correct order regardless of locale settings specified on the server.
$date = DateTime::createFromFormat('Y-m-d', '2009-08-12');
$output = $date->format('F j, Y');
date("F d, Y", strtotime($input))
$new_format = date("Your Date String", strtotime($date));
See:
- http://php.net/strtotime
- http://php.net/date
Basically, if strtotime() can read it correctly, you can reformat it anyway you please.
In this case, Year - Month - Day is a properly recognized strtotime() format, this might not be the case for other formats.
You might consider doing your date formatting in MySQL with your select statement:
DATE_FORMAT(date,'%M %e, %Y') as date_selected
http://www.w3schools.com/sql/func_date_format.asp
<?php
echo date('F j, Y', strtotime($date));
You might want to look at the php function strtotime:
http://php.net/manual/en/function.strtotime.php
It'll parse a large number of date representations to a Unix timestamp.
Then use the date function.
Using strtodate or explode to split the date into its different components, you can then use the date function with the appropriate format string:http://php.net/manual/en/function.date.php
$date = "2009-08-12";
list($year,$month,$day) = explode("-",$date);
$formattedDate = date("F d, Y", mktime(0,0,0,$month,$day,$year));
Outputs: "August 12, 2009"
<?php
//Date Formatter
/*
date: date you want to convert
format: its current format ie m-d-Y, m/d/Y, Y-m-d, Y/m/d
delimS: Current delimiter ie - or / or .
delimF: The delimiter you want for the result
NOTE: this will only convert m-d-Y to Y-m-d and back
*/
function dtform($date,$format,$delimS,$delimF){
$dateFinal = '';
if($format == 'm'.$delimS.'d'.$delimS.'Y'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[2].$delimF.$dateFinal_exp[0].$delimF.$dateFinal_exp[1];
}else if($format == 'Y'.$delimS.'m'.$delimS.'d'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[1].$delimF.$dateFinal_exp[2].$delimF.$dateFinal_exp[0];
}
return $dateFinal;
}
?>
Use it like this:
// February 1, 2005
print date ("F j, Y", mktime (0,0,0,14,1,2004));

Categories